沒想到會因為圖片剪裁中斷,以前都偷懶使用uCrop
,這次想說練習內建的剪裁,結果有夠麻煩
Android 6
如果要圖片剪裁,路徑開頭是file://
,所以要用Uri.fromFile()
把uri
轉成真實路徑
val intent = Intent("com.android.camera.action.CROP").apply {
this.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)
this.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
this.putExtra("crop", true)
this.putExtra("return-data", false)
this.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString())
this.putExtra("scale", true)
}
val cropPictureName = "005_crop_${System.currentTimeMillis()}.jpg"
val phoneFile = File(
Environment.getExternalStoragePublicDirectory("${Environment.DIRECTORY_PICTURES}/AndroidSystem"),
pictureName
)
val cropPhoneFile = File(
Environment.getExternalStoragePublicDirectory("${Environment.DIRECTORY_PICTURES}/AndroidSystem"),
cropPictureName
)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
val pictureUri = Uri.fromFile(phoneFile)
val cropUri = Uri.fromFile(cropPhoneFile)
intent.setDataAndType(pictureUri, "image/*")
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropUri)
Log.d("maho", "intent: $intent")
cropPictureResultLauncher.launch(intent)
return@registerForActivityResult
}