gpt4 book ai didi

java - Android KitKat 图像选择不返回任何东西

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:50 24 4
gpt4 key购买 nike

我一直在尝试获取在 KitKat 画廊中选择的图像的绝对图像路径,但它似乎没有成功。无论我做什么,我的变量 IMAGE_FILEPATH 总是“”。这是我的 onActivityResult()

的代码
@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);
}

怎么了?我尝试了多种解决方案,但似乎没有任何效果。

最佳答案

在 KitKat 中,Gallery 返回这样的 URI:content://com.android.providers.media.documents/document/image:1

代替:

content://media/external/images/media/1

因此,您可以在 KK 下编写以下内容以使其工作:

if (uri != null) {
try {
if( uri == null ) {
IMAGE_FILEPATH = uri.getPath();
} else {
// get the id of the image selected by the user
String wholeID = DocumentsContract.getDocumentId(data.getData());
String id = wholeID.split(":")[1];

String[] projection = { MediaStore.Images.Media.DATA };
String whereClause = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getContentResolver().query(getUri(), projection, whereClause, new String[]{id}, null);
if( cursor != null ){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
IMAGE_FILEPATH = cursor.getString(column_index);
}

cursor.close();
} else {
IMAGE_FILEPATH = uri.getPath();
}
}
} catch (Exception e) {
Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
}
}

以及我使用的函数:

private Uri getUri() {
String state = Environment.getExternalStorageState();
if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
}

return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}

这些帖子对我有帮助:retrieve absolute path when select image from gallery kitkat androidGet real path from URI, Android KitKat new storage access framework

关于java - Android KitKat 图像选择不返回任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22237028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com