[英]Not getting the image from android photos app
I have seen similar questions in SO here and here . 我在这里和这里都看到过类似的问题。
But still these didn't solve my issue, So I'm posting a new question. 但是这些仍然不能解决我的问题,所以我要发布一个新问题。
Here is my code to get path of the image selected in Photos app. 这是我的代码,用于获取在“ 照片”应用中选择的图像的路径。 But I'm not getting the cursor.getColumnIndexOrThrow(column);
但是我没有得到cursor.getColumnIndexOrThrow(column); value as 0 and cursor.getString(column_index) value as null .
值为0 ,cursor.getString(column_index)值为null 。
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
Can anybody help me to solve this issue. 谁能帮我解决这个问题。 If my question is too broad please let me know.
如果我的问题太笼统,请告诉我。 I'll update my question.
我会更新我的问题。
UPDATE: Starting intent: 更新:开始意图:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, getString(R.string.selectPhoto)), RESULT_LOAD_IMAGE);
in onActivityResult: 在onActivityResult中:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentURI, projection, null, null, null);
if (cursor == null) {
return contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(projection[0]);
return cursor.getString(idx);
}
}
Regards 问候
User below code 用户下面的代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case Common.Action_Gallery:
if (data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
}
break;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.