Get file path of image on Android

后端 未结 8 1754
长发绾君心
长发绾君心 2020-11-30 09:48

I have an app that can make pictures and upload them. The upload requires the file path of the photo but I can\'t get it.

This is my code:

public voi         


        
8条回答
  •  难免孤独
    2020-11-30 10:26

    You can do like that In Kotlin If you need kotlin code in the future

    val myUri = getImageUri(applicationContext, myBitmap!!)
    val finalFile = File(getRealPathFromURI(myUri))
    
    fun getImageUri(inContext: Context, inImage: Bitmap): Uri {
        val bytes = ByteArrayOutputStream()
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
        val path = MediaStore.Images.Media.insertImage(inContext.contentResolver, inImage, "Title", null)
        return Uri.parse(path)
    }
    
    fun getRealPathFromURI(uri: Uri): String {
        val cursor = contentResolver.query(uri, null, null, null, null)
        cursor!!.moveToFirst()
        val idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
        return cursor.getString(idx)
    }
    

提交回复
热议问题