【问题标题】:"Media not found" when trying to use Intent.ACTION_VIEW尝试使用 Intent.ACTION_VIEW 时出现“找不到媒体”
【发布时间】:2021-05-24 18:18:25
【问题描述】:

我正在使用 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

对这个问题有帮助吗?

【问题讨论】:

  • 在调用 FileProvider 之前是否使用过 file.exists()?你看过它的绝对路径吗?
  • 我没有。我会测试一下,让你知道。但我几乎可以肯定该文件存在。
  • file= /data/user/0/com.machadolemos.situations/cache/image_manager_disk_cache/ea98e9238eca3f23bc387c7943219e1d76ab97f2a13d783fed2564c287c6b31a.0
  • uri= content://com.machadolemos.situations.fileprovider/cache/image_manager_disk_cache/ea98e9238eca3f23bc387c7943219e1d76ab97f2a13d783fed2564c287c6b31a.0
  • 刚刚调试了一下,文件和uri都存在

标签: android kotlin android-intent android-glide android-image


【解决方案1】:

我未经测试的观点是你试图立即访问文件,但它可能还没有加载,所以你应该在你的 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
                }
            })

【讨论】:

  • 试过这个没有成功。使用不同的应用程序打开时,我确实注意到了一个新警告。 '该应用程序无法打开此类文件'。于是我查看了 Android Studio 中的设备管理器并检查了文件的位置。缓存的文件确实存在,但它们以 .0 结尾,所以我猜是因为扩展名不是 .jpg 或 .png,所以无法识别。
  • 我检查了这个答案stackoverflow.com/a/40303755/15259160。解决方案是创建一个新文件 .jpg 以通过 Intent 使用。我唯一担心的是,每次用户单击该图像时,它都会创建一个新文件,因此最终会得到同一张图像的多个副本。
  • 我的建议是在没有 Glide 的情况下下载文件,但通过改造或任何其他方法,将文件保存到缓存,点击后检查文件是否已经存在,如果不存在则下载,然后显示给用户,请查看有关改造的链接futurestud.io/tutorials/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 2020-08-07
  • 1970-01-01
相关资源
最近更新 更多