首页 > 解决方案 > 尝试使用 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)

尝试通过以下方式发送 Uri 的方式Intent

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

选择应用程序打开图像后 Intent 的结果是这个 toast 说 Media not found

对这个问题有任何帮助吗?

标签: androidkotlinandroid-intentandroid-glideandroid-image

解决方案


我未经测试的意见是您正在尝试立即访问文件,但它可能尚未加载,因此您应该在 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
                }
            })

推荐阅读