iT邦幫忙

2021 iThome 鐵人賽

DAY 18
0
Mobile Development

重新瞭解Android硬體控制系列 第 13

110/18 - Android 7到Android 9圖片剪裁

  • 分享至 

  • xImage
  •  

圖片剪裁到了Android 7之後,改成使用FileProvider建立的uri,就是要把路徑給FileProvider.getUriForFile()轉換一下,但我一直卡在這邊沒成功,可惜

val sharedPreferences = getSharedPreferences(BaseConstants.ANDROID_SYSTEM, MODE_P
val pictureName = sharedPreferences.getString(BaseConstants.PICTURE_NAME, "") ?: 

val intent = Intent("com.android.camera.action.CROP").apply {
    this.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_REA
    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_PICTUR
    pictureName
)

val cropPhoneFile = File(
    Environment.getExternalStoragePublicDirectory("${Environment.DIRECTORY_PICTUR
    cropPictureName
)

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {

    val pictureUri = getPictureUri(phoneFile)
    val cropUri = getPictureUri(cropPhoneFile)

    intent.setDataAndType(pictureUri, "image/*")
    intent.clipData = ClipData.newRawUri(MediaStore.EXTRA_OUTPUT, cropUri)
    intent.putExtra(MediaStore.EXTRA_OUTPUT, cropUri)
    Log.d("maho", "intent: $intent")
    cropPictureResultLauncher.launch(intent)
    return@registerForActivityResult
}

上一篇
110/17 - Android 6圖片剪裁
下一篇
110/19 - Android 10以上圖片剪裁
系列文
重新瞭解Android硬體控制14
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言