smdkdon
smdkdon

Reputation: 11

How to save an image with custom file name edited by default android gallery app intent.setAction(Intent.ACTION_EDIT)

I want to save an image which is cropped by using default gallery editor in android.

I have called the edit action by below code

intent.setAction(Intent.ACTION_EDIT);

After calling the above it redirected to the default android gallery app editor.

While saving the edited image, it saves the image with the name of 'IMG_2014' format.

Is is possible to save with custom naming convention?

Thanks in advance..

Upvotes: 1

Views: 641

Answers (1)

vijay maurya
vijay maurya

Reputation: 1

There are many ways to edit the image using third party or default app. Intent.SetAction(Intent.ACTION_EDIT) startActivity return nothing. So here is the solution to get edited image path.

val editIntent = Intent(Intent.ACTION_EDIT)
editIntent.setDataAndType(textImage.uri, "image/*")
editIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
//ContextCompat.startActivity(context, Intent.createChooser(editIntent, null), null)
(context as Activity).startActivityForResult(Intent.createChooser(editIntent, null), IMAGE_EDIT_REQUEST_CODE)

If minimum Api level more than 19, So onActivityResult data set return FileProvider content.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when(requestCode) {
                IMAGE_EDIT_REQUEST_CODE -> {
                    try {
                        val filePath = getRealPathFromUri(this, data!!.data)
                        etText?.setUpdatedImageResult(filePath)
                    } catch (ex: Exception) {

                    }
                }
                else -> return
            }
        }
    }

Content doesn't give edited image path, Then use content provider to get new saved image path.

fun getRealPathFromUri(context: Context, uri: Uri): String {
    var filePath = ""
    if (uri.host.contains("com.android.providers.media")) {
        // Image pick from recent
        val wholeID = DocumentsContract.getDocumentId(uri)
        // Split at colon, use second item in the array
        val id = wholeID.split(":")[1]

        val column = arrayOf(MediaStore.Images.Media.DATA)
        // where id is equal to
        val sel = MediaStore.Images.Media._ID + "=?"
        val cursor = context.contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,column, sel, arrayOf(id), null)
        val columnIndex = cursor.getColumnIndex(column[0])
        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex)
        }
        cursor.close()

        return filePath
    } else {
        // image pick from gallery
        return getRealPathFromUriBelowAPI11(context, uri)
    }
}

Upvotes: 0

Related Questions