Android拍照選取圖片
Android經(jīng)常會需要拍照总寻、裁剪及圖庫中選擇圖片纸厉,其實都是通過intent調(diào)用系統(tǒng)相機或者系統(tǒng)圖冊锐帜,然后在onActivityResult中捕捉返回即可祭示。
- 正常拍照選擇圖片的代碼:
例:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
在onActivityResult中通過返回的intent.getExtras().get("data")便可以獲取圖片,但Android默認可用的應用內(nèi)存大約為16M.所以Android為了不超出內(nèi)存限制辟拷,在拍照返回時通過intent返回的bitmap會被壓縮,這樣導致一直都是獲取的小圖阐斜。所以在拍照時如果想返回大圖需要通過Uri返回衫冻。
#####拍照選擇大圖代碼(tampUri為路徑饵沧,開發(fā)者文檔提供的代碼):
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定調(diào)用相機拍照后照片的儲存路徑
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
在onActivityResult中通過tempUri進行下一步操作晃财。
說明:
存儲圖片的路徑建議使用 Activity.this.getExternalCacheDir()返回的/storage/sdcard0/Android/data/<package name>/cache路徑.
通過路徑獲取 Uri 的方法,需要對不同版本兼容處理:
7.0 以下系統(tǒng): 使用 Uri.fromFile(new File(path)) 等即可.
String fileName = "temp.jpg";
File tmpFile = new File(this.getExternalCacheDir(),fileName);
Uri tmpUri = Uri.fromFile(tmpFile);
- 7.0 系統(tǒng)上,因為不允許傳遞通過 intent 傳遞 file:// 路徑, 如果設置 targetSDK 為 24(7.0), 系統(tǒng)相機則無法獲得 Uri.需要使用以下方法來獲取 Uri.
注意:AndroidManifest 中的 provider authorities 與 FileProvider.getUriForFile(); 方法中 authority 一致
如:
String authority = "com.text.images";
FileProvider.getUriForFile(mContext, authority, filePath);
<!-- grantUriPermissions:true,表示授予 URI 臨時訪問權限酌儒。 //exported:要求必須為false笤喳,為true則會報安全異常为居。 -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.text.images"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
在 AndroidManifest.xml 中設置 provider:
<provider
android:authorities="${applicationId}"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
在 res 目錄下新建 xml 目錄, 新建 provider_paths.xml 文件:
這里我使用了外部存儲 external-path, 具體標簽參考 FileProvider .
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="image_file" path="."/>
</paths>
判斷系統(tǒng)版本大于7.0時,采用 FileProvider 獲取 Uri:
tring fileName = "temp.jpg";
File tmpFile = new File(this.getExternalCacheDir(),fileName);
tmpUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, tmpFile)
為了防止Uri路徑不對導致不能保存,使用前必須確保文件路徑無誤杀狡,不然會導致“無法保存剪裁的圖片”的錯誤蒙畴。或者無提示但不能點擊確定保存圖片捣卤。正確獲取的Uri最終以file://開頭
常見問題:
設置通過Uri返回后忍抽,onActivityResult中返回的intent為null。
開發(fā)者文檔中只說了簡單調(diào)用的方法董朝,其實可以添加一些其他屬性鸠项。
intent.putExtra("return-data",false);
intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
不能添加intent.setType("image/*");,會因為找不到intent導致ActivityNotFoundException子姜。
intent.putExtra("android.intent.extras.CAMERA_FACING",1);祟绊,系統(tǒng)默認開啟的是后置攝像頭,如果希望選擇前置攝像頭可以加這句哥捕。
完整代碼:
Intent openPhotoIntent;
if (Build.VERSION.SDK_INT < 19) {
openPhotoIntent = new Intent();
openPhotoIntent.setAction(Intent.ACTION_GET_CONTENT);
openPhotoIntent.setType(mFileTypes);
} else {
openPhotoIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
openPhotoIntent.setType(mFileTypes);
}
return openPhotoIntent;
Intent openPhotoIntent;
if (Build.VERSION.SDK_INT < 19) {
openPhotoIntent = new Intent();
openPhotoIntent.setAction(Intent.ACTION_GET_CONTENT);
openPhotoIntent.setType(mFileTypes);
} else {
openPhotoIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
openPhotoIntent.setType(mFileTypes);
}
return openPhotoIntent;