尝试使用 Intent.ACTION_VIEW 时“找不到媒体”

问题描述

我正在使用 Glide 库将图像加载到 ImageView。 由于图像存储在缓存中,我尝试通过 Glide 访问它并获取它的 Uri 通过 Intent 发送以打开另一个应用程序以查看全屏图像。

我将图像加载到 imageView 的方式:

val url = GlideUrl(
              BASE_URL + "/api/files/download/?id=${list[position].fileID}",LazyHeaders.Builder()
                   .addHeader(
                       "token",SessionManager.getInstance(context)
                            ?.getSession()
                            ?.token!!
                   )
                   .build())

Glide
     .with(imageViewAttachment)
     .load(url)
     .apply(RequestOptions()
            .fitCenter()
            .format(DecodeFormat.PREFER_ARGB_8888)
            .override(Target.SIZE_ORIGINAL)
      )
      .into(imageViewAttachment)

尝试通过 Intent 发送 Uri 的方式:

val file = Glide
             .with(context)
             .asFile()
             .load(url)
             .submit()
             .get()
val uri = FileProvider
                .getUriForFile(
                       context,context.applicationContext.packageName + ".fileprovider",file
                )
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri,"image/*")
context.startActivity(intent)

这是从缓存加载文件到 ImageView 的那一刻的日志:

2021-05-24 19:04:43.310 24974-24974/com.machadolemos.situations D/Glide: Finished loading BitmapDrawable from DATA_disK_CACHE for http://qa2.machadolemos.com:60600/api/files/download/?id=1 with size [-2147483648x-2147483648] in 85.683021 ms
2021-05-24 19:04:43.801 24974-24974/com.machadolemos.situations D/Glide: Finished loading BitmapDrawable from DATA_disK_CACHE for http://qa2.machadolemos.com:60600/api/files/download/?id=5 with size [-2147483648x-2147483648] in 561.9556769999999 ms
2021-05-24 19:04:45.983 24974-25079/com.machadolemos.situations D/Glide: Finished loading File from DATA_disK_CACHE for http://qa2.machadolemos.com:60600/api/files/download/?id=5 with size [-2147483648x-2147483648] in 5.795208 ms
2021-05-24 19:04:48.545 24974-25043/com.machadolemos.situations I/Adreno: QUALCOMM build                   : 2df12b3,I07da2d9908
    Build Date                       : 10/04/18
    OpenGL ES Shader Compiler Version: EV031.25.03.01
    Local Branch                     : 
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
2021-05-24 19:04:48.545 24974-25043/com.machadolemos.situations I/Adreno: Build Config                     : S L 6.0.7 AArch64
2021-05-24 19:04:48.548 24974-25043/com.machadolemos.situations D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8953.so from current namespace instead of sphal namespace.
2021-05-24 19:04:48.552 24974-25043/com.machadolemos.situations I/Adreno: PFP: 0x005ff087,ME: 0x005ff063
2021-05-24 19:04:48.554 24974-25043/com.machadolemos.situations I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColordisplay retrieved: 0
2021-05-24 19:04:48.554 24974-25043/com.machadolemos.situations I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRdisplay retrieved: 0
2021-05-24 19:04:48.555 24974-25043/com.machadolemos.situations I/Openglrenderer: Initialized EGL,version 1.4
2021-05-24 19:04:48.555 24974-25043/com.machadolemos.situations D/Openglrenderer: Swap behavior 2

The result of the Intent after selecting app to open the image is this toast saying Media not found

对这个问题有帮助吗?

解决方法

我未经测试的意见是,当文件可能尚未加载时,您正在尝试立即访问文件,因此您应该向 Glide 调用添加一个侦听器并在 onResourceReady 回调下创建意图

Glide.with(this)
            .load(glideUrl)
            .apply(requestOptions)
            .into(object : SimpleTarget<File>(){
                override fun onResourceReady(resource: File?,transition: Transition<in File>?) {
                    //handle resource and create intent
                }
            })