程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了E / BitmapFactory:无法解码流:java.io.FileNotFoundException(没有这样的文件或目录)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决E / BitmapFactory:无法解码流:java.io.FileNotFoundException(没有这样的文件或目录)?

开发过程中遇到E / BitmapFactory:无法解码流:java.io.FileNotFoundException(没有这样的文件或目录)的问题如何解决?下面主要结合日常开发的经验,给出你关于E / BitmapFactory:无法解码流:java.io.FileNotFoundException(没有这样的文件或目录)的解决方法建议,希望对你解决E / BitmapFactory:无法解码流:java.io.FileNotFoundException(没有这样的文件或目录)有所启发或帮助;

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

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

祝好运

解决方法

我想解码从画廊拍摄的照片,但是有错误。我尝试了各种方法,但没有一个成功。请帮助修复我的代码。

这是我的代码: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)```

大佬总结

以上是大佬教程为你收集整理的E / BitmapFactory:无法解码流:java.io.FileNotFoundException(没有这样的文件或目录)全部内容,希望文章能够帮你解决E / BitmapFactory:无法解码流:java.io.FileNotFoundException(没有这样的文件或目录)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:/