調(diào)用系統(tǒng)相機(jī)拍攝并存儲
val REQUEST_CODE_TAKE_PHOTO = 1
val REQUEST_CODE_SELECT_PHOTO = 2
lateinit var imageUri : Uri
lateinit var outputImage : File
private fun takePhoto(){
//Android10.0開始鳍征,sd卡不能直接訪問,而是通過作用域存儲艳丛,也就是應(yīng)用關(guān)聯(lián)緩存目錄
outputImage = File(externalCacheDir,"myPhoto.jpg")
if (outputImage.exists()){
outputImage.delete()
}
outputImage.createNewFile()
imageUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
//當(dāng)Android7.0開始氮双,直接使用本地真是路徑的Uri被認(rèn)為是不安全的,
//會拋出一個FileUriExposeException異常送爸,需要通過FileProvider來獲取
FileProvider.getUriForFile(this,"com.example.test.fileprovider",outputImage)
}else{
Uri.fromFile(outputImage)
}
val intent = Intent("android.media.action.IMAGE_CAPTURE")
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri)
startActivityForResult(intent,REQUEST_CODE_TAKE_PHOTO)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when(requestCode){
REQUEST_CODE_TAKE_PHOTO ->{
if (resultCode == Activity.RESULT_OK){
//拍攝的圖片bitmap對象
val bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(imageUri))
//獲取一個正立的bitmap對象
checkRotate(bitmap)
}
}
}
}
/**
* 判斷圖片是否經(jīng)過旋轉(zhuǎn)
*/
private fun checkRotate(bitmap: Bitmap) : Bitmap{
val exif = ExifInterface(outputImage.path)
val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL)
return when(orientation){
ExifInterface.ORIENTATION_ROTATE_90 -> rotateBitmap(bitmap,90)
ExifInterface.ORIENTATION_ROTATE_180 -> rotateBitmap(bitmap,180)
ExifInterface.ORIENTATION_ROTATE_270 -> rotateBitmap(bitmap,270)
else -> bitmap
}
}
/**
* 旋轉(zhuǎn)圖片
*/
private fun rotateBitmap(bitmap: Bitmap, degree: Int) : Bitmap {
val matrix = Matrix()
matrix.postRotate(degree.toFloat())
val newBitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.width,bitmap.height,matrix,true)
bitmap.recycle()
return newBitmap
}
由于使用到FileProvider袭厂,還需進(jìn)行如下配置嵌器,在AndroidManifest.xml 的application標(biāo)簽內(nèi)
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.test.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
android:authorities的值必須和FileProvider.getUriForFile方法的第二參數(shù)一致谐丢。
然后還需要在res目錄創(chuàng)建xml文件夾乾忱,并在此添加file_paths.xml
<paths>
<external-path
name="external_files"
path="/" />
</paths>
name值可以隨便填,path這里用單斜線表示整個sd卡進(jìn)行共享
另外窄瘟,選擇手機(jī)圖片
//打開文件選擇器
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
//指定只顯示圖片
intent.type = "image/*"
startActivityForResult(intent,REQUEST_CODE_SELECT_PHOTO)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when(requestCode){
REQUEST_CODE_TAKE_PHOTO ->{
if (resultCode == Activity.RESULT_OK && data != null){
data.data?.let{ uri ->
val bitmap = contentResolver.openFileDescriptor(uri,"r")?.use{
BitmapFactory.decodeFileDescriptor(it.fileDescriptor)
}
}
}
}
}
}