【发布时间】:2015-02-15 19:44:36
【问题描述】:
我正在尝试从我设备的图库中获取图像... 我有一个对话框,其中有一个按钮,在用户选择照片后初始化图库,应该返回它的路径...
但路径返回为空
我就是这么干的——
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
在我的onActivityResult -
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
photo_path = getPath(selectedImageUri);
// photo_path=selectedImageUri.getPath();
Globals.galleryFlag = true;
DialogFragment pindiDialogFragment = new PinPhotoDialogFragment();
pindiDialogFragment.show(getFragmentManager(), "pin photo");
dismiss();
}
}
}
这就是我的getPath 函数 -
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATA };
Cursor cursor = getActivity.getContentResolver().query(uri,
projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
【问题讨论】:
-
你的代码有什么问题?它会崩溃,还是以不确定的方式运行?请更具体。
-
路径返回为空
-
在我看来,这有两个可能的原因:(1)您的查询没有返回任何结果,这导致光标为空。 (2) 您的
column_index与光标内容没有正确关联。这就是我要尝试的方法:在获取要返回的光标结果时,尝试return cursor.getString(1);
标签: android image-gallery onactivityresult