我的应用程序有能力选择照片从库。正是我想从这个选择文件的路径。
My app has ability to select photo from library. Exactly I want file path from this selection.
这是在code创建的意图来选择照片:
This is the code to create intent for selecting photo:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, INTENT_REQUEST_CODE_SELECT_PHOTO);
这是在code,它会从URI文件路径:
This is the code that gets file path from 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;
在谷歌照片应用程序的一切昨日的更新完全正常工作。
解析URI后现在路径
为空。
Before yesterday's update of Google Photos app everything worked perfectly fine.
Now path
is null after parsing URI.
URI是与此类似: content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F75209/ACTUAL
URI is similar to this: content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F75209/ACTUAL
我也试图创造意图与Intent.ACTION_GET_CONTENT行动 - 没有运气
I also tried to create intent with Intent.ACTION_GET_CONTENT action - no luck.
下面code为我工作,以获取内容的URI在谷歌最新的照片,以及。 我曾尝试是写入临时文件,并返回临时图像URI,如果它有权力的内容URI。
Below code is working for me to get content URI on latest Google Photos as well. What i have tried is writing to temp file and return temp image URI, if it has authority in content URI.
您可以尝试相同的:
public static String getImageUrlWithAuthority(Context context, Uri uri) {
InputStream is = null;
if (uri.getAuthority() != null) {
try {
is = context.getContentResolver().openInputStream(uri);
Bitmap bmp = BitmapFactory.decodeStream(is);
return writeToTempImageAndGetPathUri(context, bmp).toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}