Android 4.4 前後版本讀取相簿圖片和拍照完美解決方案-獲取檔案路徑
阿新 • • 發佈:2019-01-31
4.3或以下,選了圖片之後,根據Uri來做處理,很多帖子都有了,我就不詳細說了.主要是4.4,如果使用上面pick的原生方法來選圖,返回的uri還是正常的,但如果用ACTION_GET_CONTENT的方法,返回的uri跟4.3是完全不一樣的,4.3返回的是帶檔案路徑的,而4.4返回的卻是content://com.android.providers.media.documents/document/image:3951這樣的,沒有路徑,只有圖片編號的uri.這就導致接下來無法根據圖片路徑來裁剪的步驟了.
來自overflow大牛的方法,4.4得到的uri,需要以下方法來獲取檔案的路徑:
[javascript]- /**
- * <br>功能簡述:4.4及以上獲取圖片的方法
- * <br>功能詳細描述:
- * <br>注意:
- * @param context
- * @param uri
- * @return
- */
- @TargetApi(Build.VERSION_CODES.KITKAT)
- publicstatic String getPath(final Context context, final Uri uri) {
- finalboolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
- // DocumentProvider
- if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
- // ExternalStorageProvider
- if (isExternalStorageDocument(uri)) {
- final String docId = DocumentsContract.getDocumentId(uri);
- final String[] split = docId.split(“:”
- final String type = split[0];
- if (“primary”.equalsIgnoreCase(type)) {
- return Environment.getExternalStorageDirectory() + “/” + split[1];
- }
- }
- // DownloadsProvider
- elseif (isDownloadsDocument(uri)) {
- final String id = DocumentsContract.getDocumentId(uri);
- final Uri contentUri = ContentUris.withAppendedId(
- Uri.parse(”content://downloads/public_downloads”), Long.valueOf(id));
- return getDataColumn(context, contentUri, null, null);
- }
- // MediaProvider
- elseif (isMediaDocument(uri)) {
- final String docId = DocumentsContract.getDocumentId(uri);
- final String[] split = docId.split(“:”);
- final String type = split[0];
- Uri contentUri = null;
- if (“image”.equals(type)) {
- contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
- } elseif (“video”.equals(type)) {
- contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
- } elseif (“audio”.equals(type)) {
- contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
- }
- final String selection = “_id=?”;
- final String[] selectionArgs = new String[] { split[1] };
- return getDataColumn(context, contentUri, selection, selectionArgs);
- }
- }
- // MediaStore (and general)
- elseif (“content”.equalsIgnoreCase(uri.getScheme())) {
- // Return the remote address
- if (isGooglePhotosUri(uri))
- return uri.getLastPathSegment();
- return getDataColumn(context, uri, null, null);
- }
- // File
- elseif (“file”.equalsIgnoreCase(uri.getScheme())) {
- return uri.getPath();
- }
- returnnull;
- }
- publicstatic String getDataColumn(Context context, Uri uri, String selection,
- String[] selectionArgs) {
- Cursor cursor = null;
- final String column = “_data”;
- final String[] projection = { column };
- try {
- cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
- null);
- if (cursor != null && cursor.moveToFirst()) {
- finalint index = cursor.getColumnIndexOrThrow(column);
- return cursor.getString(index);
- }
- } finally {
- if (cursor != null)
- cursor.close();
- }
- returnnull;
- }
- /**
- * @param uri The Uri to check.
- * @return Whether the Uri authority is ExternalStorageProvider.
- */
- publicstaticboolean isExternalStorageDocument(Uri uri) {
- return“com.android.externalstorage.documents”.equals(uri.getAuthority());
- }
- /**
- * @param uri The Uri to check.
- * @return Whether the Uri authority is DownloadsProvider.
- */
- publicstaticboolean isDownloadsDocument(Uri uri) {
- return“com.android.providers.downloads.documents”.equals(uri.getAuthority());
- }
- /**
- * @param uri The Uri to check.
- * @return Whether the Uri authority is MediaProvider.
- */
- publicstaticboolean isMediaDocument(Uri uri) {
- return“com.android.providers.media.documents”.equals(uri.getAuthority());
- }
- /**
- * @param uri The Uri to check.
- * @return Whether the Uri authority is Google Photos.
- */
- publicstaticboolean isGooglePhotosUri(Uri uri) {
- return“com.google.android.apps.photos.content”.equals(uri.getAuthority());
- }</span></span>
/**
* <br>功能簡述:4.4及以上獲取圖片的方法
* <br>功能詳細描述:
* <br>注意:
* @param context
* @param uri
* @return
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] { split[1] };
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = { column };
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}</span></span>
為什麼會不一樣呢?
Android 4.4(含)開始,通過方式訪問相簿後,返回的Uri如下(訪問“最近”):[javascript] view plain copy print?
- Uri is:content://com.android.providers.media.documents/document/image%3A18838
- 2 Uri.getPath is :/document/image:18838
- 3 對應的圖片真實路徑:/storage/emulated/0/Pictures/Screenshots/Screenshot_2014-09-22-21-40-53.png</span>
Uri is:content://com.android.providers.media.documents/document/image%3A18838
2 Uri.getPath is :/document/image:18838
3 對應的圖片真實路徑:/storage/emulated/0/Pictures/Screenshots/Screenshot_2014-09-22-21-40-53.png</span>
不但如此,對於不同型別相簿,返回的Uri形式並不相同(訪問普通相簿):
[javascript] view plain copy print?
- Uri is:content://media/external/images/media/18822
- 2 Uri.getPath is :/external/images/media/18822
- 3 對應的圖片真實路徑:/storage/emulated/0/Download/20130224235013.jpg</span>
Uri is:content://media/external/images/media/18822
2 Uri.getPath is :/external/images/media/18822
3 對應的圖片真實路徑:/storage/emulated/0/Download/20130224235013.jpg</span>
而4.4之前返回的Uri只存在一種形式,如下:
[javascript] view plain copy print?
- Uri is:content://media/external/images/media/14046
- 2 Uri.getPath is :/external/images/media/14046
- 3 對應的圖片真實路徑:/storage/emulated/0/DCIM/Camera/20130224235013.jpg
Uri is:content://media/external/images/media/14046
2 Uri.getPath is :/external/images/media/14046
3 對應的圖片真實路徑:/storage/emulated/0/DCIM/Camera/20130224235013.jpg
因此,在Android 4.4或更高版本裝置上,通過簡單的getDataColumn(Context, Uri, null, null)進行圖片資料庫已經不能滿足所有需求,因此在獲取圖片真實路徑的時候需要根據不同型別區分對待。
版本判斷:
[javascript] view plain copy print?
- //版本比較:是否是4.4及以上版本
- finalboolean mIsKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
//版本比較:是否是4.4及以上版本
final boolean mIsKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
從相簿選擇照片方法比較:
[javascript] view plain copy print?
- /**
- * <br>功能簡述:4.4及以上從相簿選擇照片
- * <br>功能詳細描述:
- * <br>注意:
- */
- @TargetApi(Build.VERSION_CODES.KITKAT)
- privatevoid SelectImageUriAfterKikat() {
- Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
- intent.addCategory(Intent.CATEGORY_OPENABLE);
- intent.setType(”image/*”);
- startActivityForResult(intent, SELECET_A_PICTURE_AFTER_KIKAT);
- }
/**
* <br>功能簡述:4.4及以上從相簿選擇照片
* <br>功能詳細描述:
* <br>注意:
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private void SelectImageUriAfterKikat() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, SELECET_A_PICTURE_AFTER_KIKAT);
}
[javascript] view plain copy print?- /
- <br>功能簡述:4.4以下從相簿選照片並剪下
- <br>功能詳細描述:
- <br>注意:
- /
- privatevoid cropImageUri() {
- Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
- intent.setType(”image/*”);
- intent.putExtra(”crop”, “true”);
- intent.putExtra(”aspectX”, 1);
- intent.putExtra(”aspectY”, 1);
- intent.putExtra(”outputX”, 640);
- intent.putExtra(”outputY”, 640);
- intent.putExtra(”scale”, true);
- intent.putExtra(”return-data”, false);
- intent.putExtra(MediaStore.EXTRA_OUTPUT,
- Uri.fromFile(new File(IMGPATH, TMP_IMAGE_FILE_NAME)));
- intent.putExtra(”outputFormat”, Bitmap.CompressFormat.JPEG.toString());
- intent.putExtra(”noFaceDetection”, true); // no face detection
- startActivityForResult(intent, SELECT_A_PICTURE);
- }
/
* <br>功能簡述:4.4以下從相簿選照片並剪下
* <br>功能詳細描述:
* <br>注意:
*/
private void cropImageUri() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 640);
intent.putExtra("outputY", 640);
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(IMGPATH, TMP_IMAGE_FILE_NAME)));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, SELECT_A_PICTURE);
}
4.4及以上選取照片後需要呼叫剪下方法:[javascript] view plain copy print?
- /
- <br>功能簡述: 4.4及以上選取照片後剪下方法
- <br>功能詳細描述:
- <br>注意:
- @param uri
- /
- privatevoid cropImageUriAfterKikat(Uri uri) {
- Intent intent = new Intent(“com.android.camera.action.CROP”);
- intent.setDataAndType(uri, ”image/”);
- intent.putExtra(”crop”, “true”);
- intent.putExtra(”aspectX”, 1);
- intent.putExtra(”aspectY”, 1);
- intent.putExtra(”outputX”, 640);
- intent.putExtra(”outputY”, 640);
- intent.putExtra(”scale”, true);
- intent.putExtra(”return-data”, true); //返回資料bitmap
- intent.putExtra(”outputFormat”, Bitmap.CompressFormat.JPEG.toString());
- intent.putExtra(”noFaceDetection”, true); // no face detection
- startActivityForResult(intent, SET_ALBUM_PICTURE_KITKAT);
- }
/
* <br>功能簡述: 4.4及以上選取照片後剪下方法
* <br>功能詳細描述:
* <br>注意:
* @param uri
*/
private void cropImageUriAfterKikat(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 640);
intent.putExtra("outputY", 640);
intent.putExtra("scale", true);
intent.putExtra("return-data", true); //返回資料bitmap
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, SET_ALBUM_PICTURE_KITKAT);
}
拍照通用方法,對返回資料做處理: [javascript] view plain copy print?
- /
- <br>功能簡述:對拍照的圖片剪下
- <br>功能詳細描述:
- <br>注意:
- @param uri
- */
- privatevoid cameraCropImageUri(Uri uri) {
- Intent intent = new Intent(“com.android.camera.action.CROP”);
- intent.setDataAndType(uri, ”image/jpeg”);
- intent.putExtra(”crop”, “true”);
- intent.putExtra(”aspectX”, 1);
- intent.putExtra(”aspectY”, 1);
- intent.putExtra(”outputX”, 640);
- intent.putExtra(”outputY”, 640);
- intent.putExtra(”scale”, true);
- if (mIsKitKat) {
- intent.putExtra(”return-data”, true);
- } else {
- intent.putExtra(”return-data”, false);
- intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
- }
- intent.putExtra(”outputFormat”, Bitmap.CompressFormat.JPEG.toString());
- intent.putExtra(”noFaceDetection”, true);
- startActivityForResult(intent, SET_PICTURE);
- }
/
* <br>功能簡述:對拍照的圖片剪下
* <br>功能詳細描述:
* <br>注意:
* @param uri
*/
private void cameraCropImageUri(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/jpeg");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 640);
intent.putExtra("outputY", 640);
intent.putExtra("scale", true);
if (mIsKitKat) {
intent.putExtra("return-data", true);
} else {
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, SET_PICTURE);
}
顯示等處理如下:[javascript] view plain copy print?
- @Override
- protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == SELECT_A_PICTURE) {
- if (resultCode == RESULT_OK && null != data) {
- Log.i(”zou”, “4.4以下的”);
- Bitmap bitmap = decodeUriAsBitmap(Uri.fromFile(new File(IMGPATH,
- TMP_IMAGE_FILE_NAME)));
- mAcountHeadIcon.setImageBitmap(bitmap);
- }
- } elseif (requestCode == SELECET_A_PICTURE_AFTER_KIKAT) {
- if (resultCode == RESULT_OK && null != data) {
- Log.i(”zou”, “4.4以上上的”);
- mAlbumPicturePath = getPath(MainActivity.this, data.getData());
- cropImageUriAfterKikat(Uri.fromFile(new File(mAlbumPicturePath)));
- }
- } elseif (requestCode == SET_ALBUM_PICTURE_KITKAT) {
- Log.i(”zou”, “4.4以上上的 RESULT_OK”);
- Bitmap bitmap = data.getParcelableExtra(”data”);
- mAcountHeadIcon.setImageBitmap(bitmap);
- } elseif (requestCode == TAKE_A_PICTURE) {
- Log.i(”zou”, “resultCode:” + resultCode);
- cameraCropImageUri(Uri.fromFile(new File(IMGPATH, IMAGE_FILE_NAME)));
- } elseif (requestCode == SET_PICTURE) {
- Log.i(”zou”, “SET_PICTURE-resultCode:” + resultCode);
- Bitmap bitmap = null;
- if (mIsKitKat) {
- if (null != data) {
- bitmap = data.getParcelableExtra(”data”);
- }
- } else {
- bitmap = decodeUriAsBitmap(Uri.fromFile(new File(IMGPATH, IMAGE_FILE_NAME)));
- }
- mAcountHeadIcon.setImageBitmap(bitmap);
- }
- }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_A_PICTURE) {
if (resultCode == RESULT_OK && null != data) {
Log.i(“zou”, “4.4以下的”);
Bitmap bitmap = decodeUriAsBitmap(Uri.fromFile(new File(IMGPATH,
TMP_IMAGE_FILE_NAME)));
mAcountHeadIcon.setImageBitmap(bitmap);
}
} else if (requestCode == SELECET_A_PICTURE_AFTER_KIKAT) {
if (resultCode == RESULT_OK && null != data) {
Log.i(“zou”, “4.4以上上的”);
mAlbumPicturePath = getPath(MainActivity.this, data.getData());
cropImageUriAfterKikat(Uri.fromFile(new File(mAlbumPicturePath)));
}
} else if (requestCode == SET_ALBUM_PICTURE_KITKAT) {
Log.i(“zou”, “4.4以上上的 RESULT_OK”);
Bitmap bitmap = data.getParcelableExtra(“data”);
mAcountHeadIcon.setImageBitmap(bitmap);
} else if (requestCode == TAKE_A_PICTURE) {
Log.i(“zou”, “resultCode:” + resultCode);
cameraCropImageUri(Uri.fromFile(new File(IMGPATH, IMAGE_FILE_NAME)));
} else if (requestCode == SET_PICTURE) {
Log.i(“zou”, “SET_PICTURE-resultCode:” + resultCode);
Bitmap bitmap = null;
if (mIsKitKat) {
if (null != data) {
bitmap = data.getParcelableExtra(“data”);
}
} else {
bitmap = decodeUriAsBitmap(Uri.fromFile(new File(IMGPATH, IMAGE_FILE_NAME)));
}
mAcountHeadIcon.setImageBitmap(bitmap);
}
}