我从Android的文件选择器中获取了一个图像文件:Intent.ACTION_GET_CONTENT
我正在努力尝试获取它的绝对路径。
当我选择图像时,我执行此代码:
GetInfos g = new GetInfos();
Log.d("URI", g.getRealPathFromURI(data.toString()));
这是图片URI:content://com.android.providers.media.documents/document/image%3A1112
和GetInfos类:
public class GetInfos {
// The method getRealPathFromURI(imageUri):: Gives the actual filepath of the image from its contentUri:
protected String getRealPathFromURI (String contentURI) {
Uri contentUri = Uri.parse(contentURI);
Cursor cursor = (AbstractActivity.CLIENT_ACTIVITY.getContentResolver()).query(contentUri, null, null, null, null);
if (cursor == null) {
Log.d("1", "Here");
return contentUri.getPath();
} else {
Log.d("2", "Here");
cursor.moveToFirst();
int index = cursor.getColumnIndex(MediaColumns.DATA);
String i = cursor.getString(index);
cursor = null;
return i;
}
}
}
AbstractActivity.CLIENT_ACTIVITY
是运行此操作的活动(公共静态变量)
和输出:
2 : Here
E/CursorWindow(25787): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 6 columns.
你能不知道发生了什么?谢谢。