我想解码从画廊拍摄的照片,但是有错误。 我尝试了各种方法,但都没有成功。 请帮助修复我的代码。
这是我的代码: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)```
答案 0 :(得分:1)
从像这样的图库中选择图片:
import 'dart:convert';
onActivityResult像这样:
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)
}
}
祝你好运