Android启动系统相机,打开图库选择图片

版权声明:欢迎转载 有不明白的 写的不正确的地方 欢迎一起讨论 谢谢 https://blog.csdn.net/qq_27744987/article/details/52809695

相机

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//指定拍照后图片名称
String photoName = System.currentTimeMillis() + ".jpg";
//设置拍照后图片存放位置ROOT_PATH:指定位置
currentCaptureFilePath = ROOT_PATH + File.separator + photoName;
Uri imageUri = Uri.fromFile(new File(currentCaptureFilePath));
startActivityForResult(i, REQUEST_CODE_4);

onActivityResult

//拿到文件就可以得到很多你需要的信息,包括路径大小等
File captureFile = new File(currentCaptureFilePath );
//设置给ImageView控件
mImageView.setImageURI(Uri.fromFile(captureFile));

相册

Intent intent = new Intent(Intent.ACTION_PICK);
// 如果限制上传到服务器的图片类型时可以直接写如:"image/jpeg,image/png等的类型"
intent .setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent , REQUEST_CODE_3);

onActivityResult

方式1

Uri uri = data.getData();
mImageView.setImageURI(uri);

方式2

Cursor cursor = getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null); 
if (null != cursor) {
    cursor.moveToFirst();
    String path =   
    cursor.getString(cursor.getColumnIndex(
    MediaStore.Images.Media.DATA));
    cursor.close();
    File picFile = new File(path);
    mImageView.setImageURI(Uri.fromFile(picFile));
}

注意:要记得权限

<uses-permission android:name="android.permission.CAMERA" />
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />

猜你喜欢

转载自blog.csdn.net/qq_27744987/article/details/52809695
今日推荐