将图像转移到Kotlin中的其他活动 [英] Transfer image to other activity in kotlin

查看:55
本文介绍了将图像转移到Kotlin中的其他活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个类似instagram的应用程序,它将允许用户上传他们的图片,如果他们需要任何编辑,则可以按编辑按钮,然后就可以进行编辑.如何将用户选择的同一张照片转移到kotlin的编辑活动中?而且,在编辑之后,如何将整个ConstraintLayout转换为图像,然后将其传输回上载活动.

I am making an application like instagram which will allow the user to upload their pic and if they need any editing then they can press editing button and they can edit. How can I transfer the same photo which the user chose to the editing activity in kotlin? And also after editing how can I covert the whole ConstraintLayout to image and transfer it back to the upload activity.

推荐答案

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

You can save the image to internal storage from activity one, then send the image name to the second activity (via Intent) and open it in second activity.

  1. 将图像保存到内部存储器中

  1. Save the image to internal storage:

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

像这样的链接一样开始活动,而不是message放置< image_name>

Start activity like in this link and instead of message put <image_name>

在打开的活动中,获取您的< image_name>

On opened activity, get your <image_name>

从内部存储打开图像:

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()
        }
    }
}

这篇关于将图像转移到Kotlin中的其他活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆