我想解码从画廊拍摄的照片,但是有错误。我尝试了各种方法,但没有一个成功。请帮助修复我的代码。
这是我的代码: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
)
}
}
祝好运
我试着从图库中打开图像并在Imageview中显示相同的活动。我添加了WRITE_EXTERNAL_STORAGE、READ_EXTERNAL_STORAGE、internet等权限。当我从图库中选择图像并返回活动时,它会显示这种类型的错误 09-04 15:02:57.161 316 42-31642/com.androidTutorialPoint.qrcodescanner E/Bitmap
不幸的是,什么也没有发生,只是这样:无法解码流:java.io.FileNotFoundException:/com.marijan.red.messageActivity@dac48b3/document/image:256(没有这样的文件或目录) 我假设我没有正确地通过图像
如何解决它。请帮助我。提前谢谢。
我有一个定义权限的文件。该代码适用于较低版本的android,但8.0及更高版本不适用。所以我明确地要求进行自我许可检查。这段代码昨天还在工作,但突然我再次获得了相同的权限拒绝错误。
你好,我试图保存在我的应用程序上拍摄的照片,但当我试图访问内存以放置数据时,会出现错误 无法解码流java.io.FileNotFoundException/storage/emulated/0打开失败:enoint(没有这样的文件或目录) 这是我的代码。