从路径编码图像时找不到文件异常 [英] File not found exception while encoding image from path

查看:70
本文介绍了从路径编码图像时找不到文件异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像文件转换为base64.我通过如下目的启动一个活动来请求图像

I want to convert an image file to base64. I am requesting the image by starting an Activity with intent as follows

val intent = Intent(
        Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    )
intent.type = "image/*"

onActivityResult 上,我试图获取Uri结果收到的真实路径.我遵循的一种方法是在下面

On the onActivityResult I am trying to get the real path the Uri received as a result. One approach I followed is below

 fun getRealPathFromUri(context: Context, contentUri: Uri?): String {
            val result: String
            val cursor: Cursor = contentUri?.let { context.contentResolver.query(it, null, null, null, null) }!!
            cursor.moveToFirst()
            val idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
            result = cursor.getString(idx)
            cursor.close()
            return result
        
       }

通过上述方法,我得到了真实的路径

With the above approach, I get real path as

存储/仿真/0/DICM/myImagepath

storage/emulated/0/DICM/myImagepath

这是正确的吗?

是的,我还尝试在路径也不起作用之前添加 file://.

yes, I also tried adding file:// before path it is not working also.

我尝试过的另一种方法

fun getPath(context: Context, uri: Uri?): String? {
     try {
        var cursor = context.contentResolver.query(uri!!, null, null, null, null)
        cursor!!.moveToFirst()
        var document_id = cursor.getString(0)
        document_id = document_id.substring(document_id.lastIndexOf(":") + 1)
        cursor.close()
        cursor = context.contentResolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null, MediaStore.Images.Media._ID + " = ? ", arrayOf(document_id), null
        )
        cursor!!.moveToFirst()
        val path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))
        cursor.close()
        return path
    } catch (e: Exception) {
        e.printStackTrace()
        return null
    }
}

但这不会生成 document_id ,也不会给出真实路径,这可能是我的代码中的某些问题.

But this does not generate the document_id and does not give realpath, as possibly some problem in my code.

获取真实路径后,我尝试了两种解决方案将图像转换为base64

After getting real path I tried two solutions to convert image to base64

解决方案1 ​​

fun convertToBase64(imageUrl: String): String{

    val imagefile = File(imageUrl)
    var fis: FileInputStream? = null
    try {
        fis = FileInputStream(imagefile)
    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    }
    val bm = BitmapFactory.decodeStream(fis)
    val baos = ByteArrayOutputStream()
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos)
    val b: ByteArray = baos.toByteArray()

    return encodeToString(b, Base64.DEFAULT)

}

解决方案2

fun encodeBitmapToBase64String(
    image: Bitmap?,
    compressFormat: CompressFormat?,
    quality: Int
): String? {
    val byteArrayOS = ByteArrayOutputStream()
    image?.compress(compressFormat, quality, byteArrayOS)
    return encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT)
}

解决方案1抛出 FilenotFoundexception ,据我了解,这是因为找不到真正的路径.

Solution 1 throws FilenotFoundexception, as per my understanding it is because not find real path.

出于相同的原因,解决方案2返回null.

Solution 2 returns null for the same reason I guess.

我从堆栈溢出的几种最佳解决方案中找到了上述方法.但这对我不起作用.

I found the above approaches from several best solution of stack overflows answer. but it does not work for me.

我实际上不明白我的文件路径错误吗?或与base64的对话技术.

I actually do not understand that my file path was wrong? or conversation technique to base64 .

请帮助我找到最佳解决方案.

Please help me to find best solution for this.

谢谢.

推荐答案

  fun convertToBase64(imageUri: Uri):

 var is: InputStream  

  is = contentresolver.openinputstream(imageUri)

使用就像fis.

致电;

... = convertToBase64(data.data)

还是data.string?

Or is it data.string?

不要弄乱从uri获取真正的parh之类的功能.扔掉它们.

Dont mess around with functions like get real parh from uri. Throw them away.

这篇关于从路径编码图像时找不到文件异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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