我一直在嘗試獲取在KitKat畫廊中選擇的圖像的絕對圖像路徑,但它似乎並沒有工作。無論我做什麼,我的變量IMAGE_FILEPATH
總是「」。這裏是我的代碼onActivityResult()
Android KitKat圖像選擇不返回任何東西
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) return;
if (null == data) return;
Uri originalUri = null;
if (requestCode == 1) {
//JB!!!
Uri uri = data.getData();
if (uri != null) {
try {
// User had pick an image.
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver()
.query(uri,
filePathColumn, null, null, null);
cursor.moveToFirst();
IMAGE_FILEPATH = cursor.getString(0);
cursor.close();
} catch (Exception e) {
Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
}
}
} else if (requestCode == 2) {
//KK!!!
Uri uri = data.getData();
if (uri != null) {
try {
if(uri == null) {
IMAGE_FILEPATH = uri.getPath();
} else {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor != null){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
IMAGE_FILEPATH = cursor.getString(column_index);
} else {
IMAGE_FILEPATH = uri.getPath();
}
}
} catch (Exception e) {
Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
}
}
}
Resource.toast(IMAGE_FILEPATH);
super.onActivityResult(requestCode, resultCode, data);
}
怎麼回事?我嘗試了多種解決方案,似乎沒有任何工作。