我的应用程序可以从库中选择照片。我需要从此选择中获取文件路径。
以下是创建选择照片意图的代码:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, INTENT_REQUEST_CODE_SELECT_PHOTO);
这是从URI获取文件路径的代码:
Cursor cursor = null;
String path = null;
try {
String[] projection = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
int columnIndex = cursor.getColumnIndexOrThrow(projection[0]);
cursor.moveToFirst();
path = cursor.getString(columnIndex);
} finally {
if (cursor != null) {
cursor.close();
}
}
return path;
在昨天的Google Photos应用更新之前,一切都运行得非常好。
现在,在解析URI后path
为null。
URI类似于这个:content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F75209/ACTUAL
我也尝试使用Intent.ACTION_GET_CONTENT操作创建意图-没有好运。
File tempFile = new File("path_to_my_temp_directory");
中,然后将最后一个用于所有操作。 - Den Drobiazko