gpt4 book ai didi

android - 使用新的 Google 相册应用选择照片已损坏

转载 作者:IT王子 更新时间:2023-10-28 23:48:50 26 4
gpt4 key购买 nike

我的应用可以从库中选择照片。正是我想要这个选择的文件路径。

这是创建选择照片 Intent 的代码:

    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 相册应用之前,一切正常。现在 path 解析 URI 后为 null。

URI 类似于:content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F75209/ACTUAL

我还尝试使用 Intent.ACTION_GET_CONTENT 操作创建 Intent - 不走运。

最佳答案

下面的代码也可以让我在最新的 Google 照片上获取内容 URI。我尝试的是写入临时文件并返回临时图像 URI,如果它在内容 URI 中具有权限。

你也可以试试:

private 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
{
if (is != null)
{
is.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return null;
}

private 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);
}

关于android - 使用新的 Google 相册应用选择照片已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30527045/

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