【问题标题】:Transfer image to other activity in kotlin将图像传输到 kotlin 中的其他活动
【发布时间】:2020-07-01 07:15:22
【问题描述】:

我正在制作一个像 instagram 这样的应用程序,它允许用户上传他们的照片,如果他们需要任何编辑,那么他们可以按下编辑按钮并进行编辑。如何将用户选择的同一张照片传输到 kotlin 中的编辑活动?还有在编辑后如何将整个 ConstraintLayout 转换为图像并将其传输回上传活动。

【问题讨论】:

  • 我建议坚持使用 android 提供的新模型,1 个活动和片段。这样,您可以将图像保存到活动视图模型并在片段中检索它。
  • 但是我没有用fragment做到

标签: android-studio kotlin


【解决方案1】:

您可以将图像从活动一保存到内部存储,然后将图像名称发送到第二个活动(通过Intent)并在第二个活动中打开它。

  1. 将图像保存到内部存储:

    saveToInternalStorage(context, <your_bitmap>, <image_name>)

  2. link 这样开始活动,而不是 message

  3. 在打开的活动中,获取您的

  4. 从内部存储打开图像:

    val bitmap = getImageFromInternalStorage(context, <image_name>)

这是我的存储助手类:

class ImageStorageManager {
    companion object {
        fun saveToInternalStorage(context: Context, bitmapImage: Bitmap, imageFileName: String): String {
            context.openFileOutput(imageFileName, Context.MODE_PRIVATE).use { fos ->
                bitmapImage.compress(Bitmap.CompressFormat.PNG, 50, fos)
            }
            return context.filesDir.absolutePath
        }

        fun getImageFromInternalStorage(context: Context, imageFileName: String): Bitmap? {
            val directory = context.filesDir
            val file = File(directory, imageFileName)
            return if (file.exists()) {
                BitmapFactory.decodeStream(FileInputStream(file))
            } else {
                null
            }
        }

        fun deleteImageFromInternalStorage(context: Context, imageFileName: String): Boolean {
            val dir = context.filesDir
            val file = File(dir, imageFileName)
            return file.delete()
        }
    }
}

【讨论】:

  • 先生,其实我是一个初学者,我对它的了解较少。你能正确地解释一下我该怎么做吗
  • 我已经为您添加了更多详细信息。
  • 感谢先生的帮助。先生,如果您不介意,您可以再回答一个问题。 stackoverflow.com/questions/62726564/…
  • 如果它帮助您实现目标,请将其标记为已接受。
猜你喜欢
  • 2020-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 2019-12-05
相关资源
最近更新 更多