【问题标题】:Show images from folder to RecyclerView ,ImageViews,Gridview. single image and multiple将文件夹中的图像显示到 RecyclerView、ImageViews、Gridview。单个图像和多个
【发布时间】:2021-04-01 06:01:49
【问题描述】:

我想申请。 当我单击按钮创建一个文件夹并打开新活动时,我在新活动中添加一个按钮,当单击按钮时它会选择一个或多个图像选择并将其显示在Grid View 中并保存在我创建的文件夹中。 现在的问题是我想从保存该图像的文件夹中加载这些图像并显示在网格视图中。

【问题讨论】:

  • 你尝试过什么吗?
  • 是的,先生,我从过去 5 天开始尝试过
  • 我选择图像并保存在我的创建文件夹中,但我想在我的网格视图中永久显示该图像,但它不会从文件夹显示到网格视图

标签: android image gridview


【解决方案1】:

使用Activity Results API 从图库中选择Uri 的单个或多个图像:

您可以查看示例代码以供参考:

// Register Activity Result Contract
private val pickImagesLauncher =
    registerForActivityResult(GetMultipleImageContent()) { uris: List<Uri> ->
        // Do your stuff here
    }


// Call from your activity
pickImagesLauncher.launch(arrayOf("image/jpeg", "image/png"))


// Activity Result Contract for selecting multiple images of supported mime types.
class GetMultipleImageContent : ActivityResultContract<Array<String>, List<Uri>>() {
    override fun createIntent(
        context: Context,
        input: Array<String>
    ): Intent {
        return Intent(Intent.ACTION_GET_CONTENT)
            .setType("*/*")
            .addCategory(Intent.CATEGORY_OPENABLE)
            .putExtra(Intent.EXTRA_MIME_TYPES, input)
            .putExtra(Intent.EXTRA_LOCAL_ONLY, true)
            .putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
    }

    override fun getSynchronousResult(
        context: Context,
        input: Array<String>
    ): SynchronousResult<List<Uri>>? {
        return null
    }

    override fun parseResult(resultCode: Int, intent: Intent?): List<Uri> {
        return if (intent == null || resultCode != Activity.RESULT_OK) {
            emptyList()
        } else getClipDataUris(intent)
    }

    private fun getClipDataUris(intent: Intent): List<Uri> {
        // Use a LinkedHashSet to maintain any ordering that may be
        // present in the ClipData
        val resultSet = LinkedHashSet<Uri>()
        intent.data?.let { resultSet.add(it) }
        when {
            intent.clipData == null && resultSet.isEmpty() -> {
                return emptyList()
            }
        }
        intent.clipData?.let { clipData ->
            for (i in 0 until clipData.itemCount) {
                clipData.getItemAt(i).uri?.let { uri ->
                    resultSet.add(uri)
                }
            }
        }
        return ArrayList(resultSet)
    }
}

【讨论】:

    猜你喜欢
    • 2015-03-03
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多