Android 来自URI的图像路径
我正在尝试从库中获取图像文件:Android 来自URI的图像路径,android,Android,我正在尝试从库中获取图像文件: Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), GET_IMAGE_FROM_GALLERY); 消息“Selecture Picture”不会显示为祝
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
GET_IMAGE_FROM_GALLERY);
消息“Selecture Picture”不会显示为祝酒词
并在onActivityResult()中强>
Uri selectedImageUri = data.getData(); //log shows proper URI
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImageUri,
projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
cursor.getString(列索引)
返回Null
我正在Nexus4上测试它
编辑:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
GET_IMAGE_FROM_GALLERY);
看起来这是安卓4.4的一个问题,我看到其他应用也失败了
使用以下方法:
String selectedImagePath = null;
Uri selectedImageUri = data.getData();
Cursor cursor = activity.getContentResolver().query(
selectedImageUri, null, null, null, null);
if (cursor == null) {
selectedImagePath = selectedImageUri.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
selectedImagePath = cursor.getString(idx);
}
可能更好的可靠答案:我在使用nexus和moto g时遇到游标null异常。我从一个有1行6列的游标窗口中读取第0行第1列失败。错误。