本文介绍了从URI映像路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从画廊的图片文件:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
        GET_IMAGE_FROM_GALLERY);

消息Selecture图片不作为敬酒。

The message "Selecture Picture" is not shown as a Toast.

而在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(Column_Index中) 返回Null

我在Nexus 4测试。

I am testing it on Nexus 4.

编辑:

看起来这是与Android 4.4的问题,我看其他的应用程序失败了。

Looks like this is a problem with Android 4.4, I have see other apps failing too.

Convert内容:// URI实际路径的Andr​​oid 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);
}

这篇关于从URI映像路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:38