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.
Asked
Active
Viewed 323 times
0
-
I would recommend to stick to the new model android provides, 1 activity and fragments. This way you can save the image to activity viewmodel and retrieve it inside of the fragment. – Daniel Jul 01 '20 at 07:18
-
But I have not made it with fragment – Abhinash Mallick Jul 01 '20 at 07:21
1 Answers
0
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.
Save the image to internal storage:
saveToInternalStorage(context, <your_bitmap>, <image_name>)
Start activity like in this link and instead of
message
put <image_name>On opened activity, get your <image_name>
Open the image from internal storage:
val bitmap = getImageFromInternalStorage(context, <image_name>)
Here is my storage helper class:
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()
}
}
}

Simon
- 1,657
- 11
- 16
-
Sir actually I am a beginner and I have less knowledge about it. Can u explain me properly like how will I do – Abhinash Mallick Jul 02 '20 at 08:20
-
-
Thank you sir for ur help. Sir if u do not mind can u answer one more question. https://stackoverflow.com/questions/62726564/profile-fragment-code-not-working-in-kotlin – Abhinash Mallick Jul 04 '20 at 14:36
-