E/BitmapFactory:无法解码流:java.io.FileNotFoundException(没有这样的文件或目录) [英] E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException (No such file or directory)
本文介绍了E/BitmapFactory:无法解码流:java.io.FileNotFoundException(没有这样的文件或目录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对我从画廊拍摄的照片进行解码,但是有错误. 我尝试了各种方法,但都没有成功. 请帮助修复我的代码.
I want to decode the picture I took from the gallery but it has an error. I have tried various methods but none have been successful. Please help to fix my code.
这是我的代码: 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的结果
This is the result of 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)```
推荐答案
从像这样的画廊中选择图片:
Select image from gallery fun like this:
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像这样:
onActivityResult like this:
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)
}
}
获取像这样的位图扩展名:
Get bitmap extension like this:
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
)
}
}
祝你好运
这篇关于E/BitmapFactory:无法解码流:java.io.FileNotFoundException(没有这样的文件或目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文