之前已经问过有关Android Q和Glide的问题,但我无法将它们放在一起,所以我希望有人愿意提供帮助。
我正在将图像从云端(Firebase)下载或从图库中选择或使用相机拍摄的图像保存到本地存储。我曾经将其保存在应用程序的文件夹中,并且可以将其添加到图库中,但是使用Android Q不再可行。因此,我更改了应用程序以使用MediaStore和ContentResolver来访问文件并保存它。文件保存在Pictures
的MediaStore.Images.Media.EXTERNAL_CONTENT_URI
目录中。
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: Unknown or unsupported URL: content://
。而且我似乎无法弄清楚原因。它曾经可以工作,并且URI应该正确,因为它也可以在其他地方使用(上传文件,保存文件等)并且可以工作。 Glide /我的代码有什么问题?
完整错误:
E/GlideExecutor: Request threw uncaught throwable
java.lang.UnsupportedOperationException: Unknown or unsupported URL: content://media/external/images/media/Pictures/ForestLog/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()
错误。查询_ID
,然后使用ContentUris.withAppendedId()
组装要使用的Uri
。
例如,在this sample app中的this book中,我使用:
suspend fun getLocalUri(filename: String): Uri? =
withContext(Dispatchers.IO) {
val resolver = context.contentResolver
resolver.query(collection, PROJECTION, QUERY, arrayOf(filename), null)
?.use { cursor ->
if (cursor.count > 0) {
cursor.moveToFirst()
return@withContext ContentUris.withAppendedId(
collection,
cursor.getLong(0)
)
}
}
null
}
其中PROJECTION
和QUERY
是:
private val PROJECTION = arrayOf(MediaStore.Video.Media._ID)
private const val QUERY = MediaStore.Video.Media.DISPLAY_NAME + " = ?"
[collection
在哪里:
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
代替]