java通过从MediaStore中拾取图像来创建文件。动作_图像_捕获
我允许用户从他们的照片库中选择图像:
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent }
);
startActivityForResult(chooserIntent, SELECT_PICTURE);
在onActivityResult
中,我想从所选图像创建一个新的File
。以下是我正在做的:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
if (data == null) {
return;
}
Uri selectedImageUri = data.getData();
File mediaFile = new File(selectedImageUri.getPath());
if(!mediaFile.exists())
Log.d("File", "File doesn't exist");
}
}
我的支票返回错误。我做错了什么
更新
我也试过这样做:
String fullPath = getRealPathFromURI(selectedImageUri);
使用来自enter link description here问题的此方法:
public String getRealPathFromURI(Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
但是,该方法返回null
# 1 楼答案
将该类添加到项目中
并使用安卓api的相对方法从uri中获取图像路径
编辑::检查手机的SDK版本
并使用
来自this的引用
# 2 楼答案
希望对你有帮助