【问题标题】:open image recently created with Intent.ACTION_VIEW in android打开最近在 android 中使用 Intent.ACTION_VIEW 创建的图像
【发布时间】:2021-06-23 22:33:31
【问题描述】:

我有一个这样的任务,如果用户要下载图片,他会点击蓝色按钮,图片会被下载,并且会显示小吃栏,如果用户点击了小吃店图片将打开

问题是当用户点击小吃店的动作时我看不到图像,只会显示一个黑屏,图像已创建,我可以使用我的文件浏览器和我的画廊看到它

这是我用来下载图片的代码

    val picturesDirectory: File =
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)

            try {
                var localFile = File(picturesDirectory, "picture.jpg")
                val out = FileOutputStream(localFile)
               
                  val bitmapDrawable = imageView.drawable as BitmapDrawable
                    bitmapDrawable.bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
                    out.flush()
                    out.close()
                }
                context.sendBroadcast(
                    Intent(
                        Intent.ACTION_MEDIA_MOUNTED,
                        Uri.parse("file://" + Environment.getExternalStorageDirectory())
                    )
                )

            } catch (e: Exception) {
                Log.d(TAG, "getPhotoFile: ${e.message}")
            }

以及用来显示图片的部分

 val picturesDirectory: File =
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
              
               val image = File(picturesDirectory,"picture.jpg")
                  val intent = Intent()
                  intent.action = Intent.ACTION_VIEW
                  intent.setDataAndType(Uri.fromFile(image), "image/*")
                  intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                  intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
                  startActivity(intent)

正如我所说,图像已创建,路径相同但查看器无法显示, 我试图用相同的意图查看器代码显示另一个图像,它正常显示,所以问题是它无法显示最近创建的图像。 我也尝试使用 FileProvider,这不是安全或限制问题,因为我使用 SDK 21 测试应用程序 任何解决方案? 更新:当我用文件资源管理器重命名图像时,它会正常显示

【问题讨论】:

  • 我猜你应该使用FileProvider.getUriForFile
  • 我试过了,这不是限制的问题,因为我用另一个文件(不是最近创建的)尝试了没有 FileProvider 并且它有效

标签: android image android-intent android-file actionview


【解决方案1】:

我解决了,我必须添加

         val contentUri = Uri.fromFile(photoFile)
                val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
                mediaScanIntent.data = contentUri
                requireActivity().sendBroadcast(mediaScanIntent)

创建文件后

【讨论】:

    猜你喜欢
    • 2014-06-14
    • 2016-05-22
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多