使用“从URI显示图像”;内容:/“;androidq上的Glide方案

使用“从URI显示图像”;内容:/“;androidq上的Glide方案,android,android-glide,android-contentresolver,mediastore,android-10.0,Android,Android Glide,Android Contentresolver,Mediastore,Android 10.0,以前有人问过安卓Q和Glide的问题,但我不能把它们全部放在一起,所以我希望有人愿意帮忙 我正在将从云(Firebase)下载、从gallery中选择或使用相机拍摄的图像保存到本地存储。我曾经将它保存在应用程序的文件夹中,并可以将其添加到图库中,但在安卓Q中,这已经不可能了。所以我改变了我的应用程序,使用MediaStore和ContentResolver来访问文件并保存它。文件保存在MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI的Picture

以前有人问过安卓Q和Glide的问题,但我不能把它们全部放在一起,所以我希望有人愿意帮忙

我正在将从云(Firebase)下载、从gallery中选择或使用相机拍摄的图像保存到本地存储。我曾经将它保存在应用程序的文件夹中,并可以将其添加到图库中,但在安卓Q中,这已经不可能了。所以我改变了我的应用程序,使用MediaStore和ContentResolver来访问文件并保存它。文件保存在
MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI
Pictures
目录中

StorageHandler:

public Uri createUriForFile(String fileName, String parentId)
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
    {
        Uri uriFromContentResolver = getUriFromContentResolver(fileName);
        if(uriFromContentResolver != null)
        {
            return uriFromContentResolver;
        }else
        {
            ContentResolver resolver = baseActivity.getApplicationContext().getContentResolver();
            ContentValues contentValues = new ContentValues();
            contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
            contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
            contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, EXTERNAL_PICTURE_RELATIVE_PATH);
            return resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        }
    }else {
        File pictureFile  = new File( EXTERNAL_PICTURE_STORAGE_DIR.getAbsolutePath() + File.separator + parentId + File.separator + fileName );
        checkAndCreatePictureDir(parentId);
        return FileProvider.getUriForFile(baseActivity, baseActivity.getApplicationContext().getPackageName() + ".fileprovider", pictureFile);
    }
}

public Uri getUriFromContentResolver(String fileName)
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
    {
        ContentResolver resolver = baseActivity.getApplicationContext().getContentResolver();

        Cursor queryCursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DISPLAY_NAME,
                        MediaStore.Images.Media.RELATIVE_PATH}, MediaStore.Images.Media.DISPLAY_NAME + "=? ",
                new String[]{fileName}, null);

        if (queryCursor != null && queryCursor.moveToFirst())
        {
            Uri content = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.buildUpon().appendEncodedPath(queryCursor.getString(queryCursor.getColumnIndex(MediaStore.Images.Media.RELATIVE_PATH)))
                    .appendPath(queryCursor.getString(queryCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME))).build();
            return content;
        } else
        {
            return null;
        }
    }else
    {
        return null;
    }
它将文件保存到存储器中,可以通过Android Studio中的设备浏览器查看。它也被上传到云上,并且可以从那里查看(如果它是一个新文件)。但是,当我试图使用Glide将其加载到
ImageView
时,它给了我一个错误:
java.lang.UnsupportedOperationException:未知或不受支持的URL:content://
。我似乎不明白为什么。它曾经工作过,URI应该是正确的,因为它在其他地方也使用过(上传文件、保存文件等),而且它工作正常。Glide/我的代码有什么问题

完全错误:

E/GlideExecutor: Request threw uncaught throwable
java.lang.UnsupportedOperationException: Unknown or unsupported URL: content://media/external/images/media/Pictures/**AppName**/IMG20200112164646.jpg
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
    at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:151)
    at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:705)
    at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1687)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1503)
    at android.content.ContentResolver.openInputStream(ContentResolver.java:1187)
    at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResourceFromUri(StreamLocalUriFetcher.java:85)
    at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResource(StreamLocalUriFetcher.java:60)
    at com.bumptech.glide.load.data.StreamLocalUriFetcher.loadResource(StreamLocalUriFetcher.java:15)
    at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44)
    at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
    at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:302)
    at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:272)
    at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:233)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:919)
    at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:446)

您的
getUriFromContentResolver()
错误。查询
\u ID
,然后使用
ContentUris.withAppendedId()
组合要使用的
Uri

例如,在from中,我使用:

其中
投影
查询
是:

private val PROJECTION = arrayOf(MediaStore.Video.Media._ID)
private const val QUERY = MediaStore.Video.Media.DISPLAY_NAME + " = ?"
其中,
集合
是:

  private val collection =
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) MediaStore.Video.Media.getContentUri(
      MediaStore.VOLUME_EXTERNAL
    ) else MediaStore.Video.Media.EXTERNAL_CONTENT_URI

(在我的例子中,我正在查询视频;您将使用
MediaStore.Images

您的
getUriFromContentResolver()
是错误的。查询
\u ID
,然后使用
ContentUris.withAppendedId()
组合要使用的
Uri

例如,在from中,我使用:

其中
投影
查询
是:

private val PROJECTION = arrayOf(MediaStore.Video.Media._ID)
private const val QUERY = MediaStore.Video.Media.DISPLAY_NAME + " = ?"
其中,
集合
是:

  private val collection =
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) MediaStore.Video.Media.getContentUri(
      MediaStore.VOLUME_EXTERNAL
    ) else MediaStore.Video.Media.EXTERNAL_CONTENT_URI

(在我的例子中,我正在查询视频;您将使用
MediaStore.Images

尝试使用和
fromsingeluri
fromTreeUri
而不是File类?错误不在我使用
File
类的部分,该部分仅在Android Q之前的版本中使用,这仅仅是我使用
ContentResolver
MediaStore
的部分,我无法使用它们。使用
文件
绝对路径
在Android Q中是不推荐的,只能通过启用传统存储来实现。我不想这样,因为我需要为将来的版本修复它。对不起,我误读了你的问题。我的坏:)没问题,先生,当有人花时间写响应时,我总是很感激。尝试使用和
fromsingeluri
fromTreeUri
而不是File类?错误不在我使用
File
类的部分,它只在Android Q之前的版本中使用。这部分有效,这仅仅是我使用
ContentResolver
MediaStore
的部分,我无法使用它们。使用
文件
绝对路径
在Android Q中是不推荐的,只能通过启用传统存储来实现。我不想这样,因为我需要为将来的版本修复它。对不起,我误读了你的问题。我的坏:)没问题,先生,当有人花时间写回复的时候,总是很感激。哦,天哪,这太愚蠢了。几天前我已经试过了,但没有成功(代码中有其他错误)。在完成所有工作后,我忘记了我需要再试一次,看看这是否有帮助。谢谢你的回答!天哪,这太蠢了。几天前我已经试过了,但没有成功(代码中有其他错误)。在完成所有工作后,我忘记了我需要再试一次,看看这是否有帮助。谢谢你的回答!