当前位置: 首页 > 面试题库 >

E / BitmapFactory:无法解码流:java.io.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)```

问题答案:

从画廊的乐趣中选择图像,如下所示:

    private fun selectImageFromGallery() {

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

onActivityResult像这样:

 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
    )
  }
}

祝好运



 类似资料: