使用相机和图库

1、不用权限

2、启动照相机和图库

static final int TAKE_AVATAR_CAMERA_REQUEST = 1;//图库
static
final int TAKE_AVATAR_GALLERY_REQUEST = 2;//照相机

//启动照相机

String strAvatarPrompt = "照个照片!";
Intent pictureIntent=new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(Intent.createChooser(pictureIntent,
strAvatarPrompt),
TAKE_AVATAR_CAMERA_REQUEST);//启动照相机

//启动图库

Intent pickPhoto=new
Intent(Intent.ACTION_PICK);
pickPhoto.setType("image/*");
startActivityForResult(pickPhoto,
TAKE_AVATAR_GALLERY_REQUEST);//启动图库选照片

//在onActivityResult获得返回的照片

protected void onActivityResult(int requestCode,int
resultCode,Intent data) {
int maxLength=150;
switch
(requestCode) {
case
TAKE_AVATAR_CAMERA_REQUEST://照相机
if(resultCode==Activity.RESULT_OK){
Bitmap

cameraPic=(Bitmap)data.getExtras().get("data");//取得照片
saveBitmap(cameraPic);//保存照片到应用程序
}else
if(resultCode==Activity.RESULT_CANCELED)
break;
case
TAKE_AVATAR_GALLERY_REQUEST://图库
if(resultCode==Activity.RESULT_OK){
Uri
photoUri=data.getData();//取得照片
try {
Bitmap
galleryPic=android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(),
photoUri);
saveBitmap(galleryPic);//保存照片到应用程序
} catch
(FileNotFoundException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch
block
e.printStackTrace();
}
}else
if(resultCode==Activity.RESULT_CANCELED)
break;
default:
break;
}
}

//保存图片到应用程序

private void saveBitmap(Bitmap avatar) {
String
strAvatarFilename = "avatar.jpg";
try
{
avatar.compress(CompressFormat.JPEG,
100,
openFileOutput(strAvatarFilename,
MODE_PRIVATE));
} catch (Exception e) {
Log.e(DEBUG_TAG,
"Avatar compression and save failed.", e);
}

Uri imageUriToSaveCameraImageTo = Uri.fromFile(new
File(
QuizSettingsActivity.this.getFilesDir(),
strAvatarFilename));

// Update the settings screen
ImageButton avatarButton =
(ImageButton) findViewById(R.id.ImageButton_Avatar);
String
strAvatarUri
= imageUriToSaveCameraImageTo.getPath();//取得图片的Uri
Uri
imageUri =
Uri.parse(strAvatarUri);
avatarButton.setImageURI(null); //
Workaround for refreshing an
// ImageButton, which
tries to cache the
// previous image Uri. Passing
null
// effectively resets
it.
avatarButton.setImageURI(imageUri);
}

时间: 2024-10-25 12:55:10

使用相机和图库的相关文章

上传文件 从相机、图库获取与截取图片

Activity /**实际开发涉及文件上传.下载都不会自己写这些代码,一般会集成第三方库来完成,比如android-async-http,okhttp,xUtils等*/ public class UploadFileActivity extends ListActivity {     private ImageView imageView;     private TextView tv_info;     public static final int uid = 10415362;  

邮件、短信、相机、图库的使用

-.首先导入MessageUI.framework框架 二.导入头文件#import <MessageUI/MessageUI.h>和代理方FMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate 三. .h文件 #import <UIKit/UIKit.h> #im

调用系统的相机和图库,并裁剪

1 private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照 2 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 3 private static final int PHOTO_REQUEST_CUT = 3;// 结果 4 private File tempFile = new File(Environment.getExternalStorageDirect

[Android Pro] 调用系统相机和图库,裁剪图片

private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 private static final int PHOTO_REQUEST_CUT = 3;// 结果 private File tempFile = new File(Environment.getExternalStorageDirectory(), g

调用系统相机和图库,裁剪图片

private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 private static final int PHOTO_REQUEST_CUT = 3;// 结果 private File tempFile = new File(Environment.getExternalStorageDirectory(), g

Android 相机与图库

1 import android.content.Intent; 2 import android.graphics.Bitmap; 3 import android.graphics.BitmapFactory; 4 import android.net.Uri; 5 import android.os.Environment; 6 import android.provider.MediaStore; 7 import android.support.v7.app.AppCompatActi

使用Android Studio进行安卓开发教程

本教程介绍如何创建Android应用程序.它基于Android5.0(Lollipop)介绍Android Studio的用法. 安卓介绍 Android是基于Linux内核的操作系统.负责开发Android系统的项目被为Android Open Source Project (AOSP) ,由谷歌领导. Android系统支持后台处理,提供了丰富的用户界面库,使用的OpenGL标准支持2-D和3-D图形,并允许访问文件系统以及嵌入式SQLite数据库. Android应用包含可见和不可见组件,

android源码大放送(实战开发必备),免费安卓demo源码,例子大全文件详细列表

免费安卓demo源码,例子大全文件详细列表 本列表源码永久免费下载地址:http://www.jiandaima.com/blog/android-demo 卷 yunpan 的文件夹 PATH 列表 卷序列号为 0000-73EC E:. │ jiandaima.com文件列表生成.bat │ 例子大全说明.txt │ 本例子永久更新地址~.url │ 目录列表2016.03.10更新.txt │ ├─前台界面 │ ├─3D标签云卡片热门 │ │ Android TagCloudView云标签

iOS红马甲项目Bug总结(2)

背景:iOS调用相机和访问图库 一.调用相机或图库: -(void)imgviewClick { ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus]; AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; UIAlertController *alertvc=[