1、一个很好用的权限框架
implementation "com.github.permissions-dispatcher:permissionsdispatcher:4.9.1"
kapt "com.github.permissions-dispatcher:permissionsdispatcher-processor:4.9.1"
@OnNeverAskAgain(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
fun onOpenAlbumNeverAskAgain() {
AlertDialog.Builder(this)
.setCancelable(true)
.setTitle(getString(R.string.tip))
.setMessage(getString(R.string.close_write_read_permission_tip))
.setNegativeButton(getString(R.string.cancel)) { _, _ ->
}
.setPositiveButton(getString(R.string.go_setting)) { _, _ ->
PermissionUtils.startSystemIntent(this)
}
.create()
.show()
}
2、使用手机自带的相机拍照并获得结果(参考Android:调用系统相机实现拍照+裁切(兼容 7.0,11.0) - 简书)
@NeedsPermission(
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
fun takePhotos() {
imageFile = createImageFile()
imageFile?.let {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //如果是7.0以上,使用FileProvider,否则会报错
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
imgUri = FileProvider.getUriForFile(this, AUTHORITY, it)
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri) //设置拍照后图片保存的位置
} else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(it)) //设置拍照后图片保存的位置
}
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()) //设置图片保存的格式
intent.resolveActivity(packageManager)?.let {
activityResultLauncher.launch(intent) //调起系统相机
}
}
}
private var imageFile: File? = null
private var imgUri: Uri? = null
private val AUTHORITY = "com.transportation.camera.fileProvider" //FileProvider的签名(后面会介绍)
private val activityResultLauncher: ActivityResultLauncher<Intent> =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
imgUri?.let {
i1 ->
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val savePath = "${getCacheFilePath(this)}/IMG_$timeStamp.jpg"
launch(Dispatchers.IO, {
val bitmap = getImage(FileUtil.getFilePathByUri(this@UploadActivity, i1))
Timber.d("savePath = $savePath")
bitmap?.let { b ->
saveBitmap(b, savePath)
}
withContext(Dispatchers.Main) {
imageUri.add(0, savePath)
uploadImageAdapter.notifyDataSetChanged()
if (imageUri.size > 1) {
binding.ivFinish.visibility = View.VISIBLE
}
}
}, { it2 ->
it2.printStackTrace()})
// imageUri.add(0 ,i1.toString())
}
}
}
private fun saveBitmap(bitmap: Bitmap, path: String) {
try {
val file = File(path)
if (!file.exists()) {
file.getParentFile().mkdirs()
file.createNewFile()
val fos = FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
fos.flush()
fos.close()
}
}catch (e: IOException) {
e.printStackTrace()
return;
}
}
//根据路径获取用户选择的图片
private fun getImage(imgPath: String): Bitmap? {
val options = BitmapFactory.Options()
options.inSampleSize = 2 //直接设置它的压缩率,表示1/2
var b: Bitmap? = null
try {
val a = BitmapFactory.decodeFile(imgPath, options)
val matrix = Matrix()
a?.let {
b = Bitmap.createBitmap(it, 0, 0, it.width, it.height, matrix, true)
}
} catch (e: Exception) {
e.printStackTrace()
}
return b
}
|