当前位置: 首页 > 知识库问答 >
问题:

E/BitmapFactory:无法解码流:java。伊奥。FileNotFoundException(无此类文件或目录)

云飞翮
2023-03-14

我想解码我从画廊拍的照片,但它有一个错误。我尝试过各种方法,但都没有成功。请帮我修改代码。

这是我的代码:https://pastebin.com/syWjqPDK

class TambahDataActivity : AppCompatActivity() {

private val GALLERY_REQUEST_CODE = 101

private var encoded_string: String? = null
private var image_name: String? = null
private var file: File? = null
private var file_uri: Uri? = null
private var bitmap: Bitmap? = null

private val STORAGE_PERMISSION_CODE = 1

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_tambah_data)
    btn_tambah_foto.setOnClickListener {
        if (ContextCompat.checkSelfPermission(applicationContext,
            Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
            val intent = Intent(Intent.ACTION_PICK)
            intent.type = "image/*"
            intent.putExtra(MediaStore.EXTRA_OUTPUT, file_uri)
            startActivityForResult(intent, GALLERY_REQUEST_CODE)
        } else {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), STORAGE_PERMISSION_CODE)
        }
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK) when (requestCode) {
        GALLERY_REQUEST_CODE -> {
            //data.getData returns the content URI for the selected Image
            file_uri = data?.data!!
            val fileee: String = data.data?.lastPathSegment!!
            val filename: String = fileee.substring(fileee.lastIndexOf("/") + 1)
            img_adu.setImageURI(file_uri)
            img_adu.visibility = View.VISIBLE

            file = File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    .toString() + File.separator + filename
            )
            file_uri = Uri.fromFile(file)
            bitmap = BitmapFactory.decodeFile(file_uri!!.path)
            val stream = ByteArrayOutputStream()
                bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, stream)
                bitmap?.recycle()
                val array = stream.toByteArray()
                encoded_string = Base64.encodeToString(array, 0)

        }
    }
}

}

这是logcat的结果

2020-03-19 14:03:10.735 10607-10607/tgs.app.pengaduan E/fileUrii: /storage/emulated/0/Pictures/IMG_20200319_100639.jpg
2020-03-19 14:03:10.736 10607-10607/tgs.app.pengaduan E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/IMG_20200319_100639.jpg (No such file or directory)```

共有1个答案

栾烨华
2023-03-14

从gallery fun中选择图像,如下所示:

    private fun selectImageFromGallery() {

    val intent = Intent()
    intent.type = "image/*"
    intent.action = Intent.ACTION_GET_CONTENT
    startActivityForResult(
        Intent.createChooser(
            intent,
            "Please select..."
        ),
        GALLERY_REQUEST_CODE
    )
}

像这样的结果:

 override fun onActivityResult(
    requestCode: Int,
    resultCode: Int,
    data: Intent?
) {

    super.onActivityResult(
        requestCode,
        resultCode,
        data
    )

    if (requestCode == GALLERY_REQUEST_CODE
        && resultCode == Activity.RESULT_OK
        && data != null
        && data.data != null
    ) {

        // Get the Uri of data
        val file_uri = data.data
        img_adu.setImageURI(file_uri)
        img_adu.visibility = View.VISIBLE
        bitmap = file_uri?.getCapturedImage(applicationContext)
        val stream = ByteArrayOutputStream()
            bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, stream)
            bitmap?.recycle()
            val array = stream.toByteArray()
            encoded_string = Base64.encodeToString(array, 0)
    }
 }

获取位图扩展,如下所示:

fun Uri.getCapturedImage(context: Context): Bitmap? {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    val source =
        context?.contentResolver?.let { ImageDecoder.createSource(it, this) }
    return source?.let { ImageDecoder.decodeBitmap(it) }
} else {
    return MediaStore.Images.Media.getBitmap(
        context?.contentResolver,
        this
    )
  }
}

祝你好运

 类似资料: