1. 程式人生 > >Android 4.4 前後版本讀取相簿圖片和拍照完美解決方案-獲取檔案路徑

Android 4.4 前後版本讀取相簿圖片和拍照完美解決方案-獲取檔案路徑

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]
view plain copy print?
  1. /**  
  2.  * <br>功能簡述:4.4及以上獲取圖片的方法 
  3.  * <br>功能詳細描述: 
  4.  * <br>注意: 
  5.  * @param context 
  6.  * @param uri 
  7.  * @return 
  8.  */
  9. @TargetApi(Build.VERSION_CODES.KITKAT)  
  10. publicstatic String getPath(final Context context, final Uri uri) {  
  11.     finalboolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;  
  12.     // DocumentProvider
  13.     if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {  
  14.         // ExternalStorageProvider
  15.         if (isExternalStorageDocument(uri)) {  
  16.             final String docId = DocumentsContract.getDocumentId(uri);  
  17.             final String[] split = docId.split(“:”
    );  
  18.             final String type = split[0];  
  19.             if (“primary”.equalsIgnoreCase(type)) {  
  20.                 return Environment.getExternalStorageDirectory() + “/” + split[1];  
  21.             }  
  22.         }  
  23.         // DownloadsProvider
  24.         elseif (isDownloadsDocument(uri)) {  
  25.             final String id = DocumentsContract.getDocumentId(uri);  
  26.             final Uri contentUri = ContentUris.withAppendedId(  
  27.                     Uri.parse(”content://downloads/public_downloads”), Long.valueOf(id));  
  28.             return getDataColumn(context, contentUri, nullnull);  
  29.         }  
  30.         // MediaProvider
  31.         elseif (isMediaDocument(uri)) {  
  32.             final String docId = DocumentsContract.getDocumentId(uri);  
  33.             final String[] split = docId.split(“:”);  
  34.             final String type = split[0];  
  35.             Uri contentUri = null;  
  36.             if (“image”.equals(type)) {  
  37.                 contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;  
  38.             } elseif (“video”.equals(type)) {  
  39.                 contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;  
  40.             } elseif (“audio”.equals(type)) {  
  41.                 contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;  
  42.             }  
  43.             final String selection = “_id=?”;  
  44.             final String[] selectionArgs = new String[] { split[1] };  
  45.             return getDataColumn(context, contentUri, selection, selectionArgs);  
  46.         }  
  47.     }  
  48.     // MediaStore (and general)
  49.     elseif (“content”.equalsIgnoreCase(uri.getScheme())) {  
  50.         // Return the remote address
  51.         if (isGooglePhotosUri(uri))  
  52.             return uri.getLastPathSegment();  
  53.         return getDataColumn(context, uri, nullnull);  
  54.     }  
  55.     // File
  56.     elseif (“file”.equalsIgnoreCase(uri.getScheme())) {  
  57.         return uri.getPath();  
  58.     }  
  59.     returnnull;  
  60. }  
  61. publicstatic String getDataColumn(Context context, Uri uri, String selection,  
  62.         String[] selectionArgs) {  
  63.     Cursor cursor = null;  
  64.     final String column = “_data”;  
  65.     final String[] projection = { column };  
  66.     try {  
  67.         cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,  
  68.                 null);  
  69.         if (cursor != null && cursor.moveToFirst()) {  
  70.             finalint index = cursor.getColumnIndexOrThrow(column);  
  71.             return cursor.getString(index);  
  72.         }  
  73.     } finally {  
  74.         if (cursor != null)  
  75.             cursor.close();  
  76.     }  
  77.     returnnull;  
  78. }  
  79. /** 
  80.  * @param uri The Uri to check. 
  81.  * @return Whether the Uri authority is ExternalStorageProvider. 
  82.  */
  83. publicstaticboolean isExternalStorageDocument(Uri uri) {  
  84.     return“com.android.externalstorage.documents”.equals(uri.getAuthority());  
  85. }  
  86. /** 
  87.  * @param uri The Uri to check. 
  88.  * @return Whether the Uri authority is DownloadsProvider. 
  89.  */
  90. publicstaticboolean isDownloadsDocument(Uri uri) {  
  91.     return“com.android.providers.downloads.documents”.equals(uri.getAuthority());  
  92. }  
  93. /** 
  94.  * @param uri The Uri to check. 
  95.  * @return Whether the Uri authority is MediaProvider. 
  96.  */
  97. publicstaticboolean isMediaDocument(Uri uri) {  
  98.     return“com.android.providers.media.documents”.equals(uri.getAuthority());  
  99. }  
  100. /** 
  101.  * @param uri The Uri to check. 
  102.  * @return Whether the Uri authority is Google Photos. 
  103.  */
  104. publicstaticboolean isGooglePhotosUri(Uri uri) {  
  105.     return“com.google.android.apps.photos.content”.equals(uri.getAuthority());  
  106. }</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?
  1. Uri is:content://com.android.providers.media.documents/document/image%3A18838
  2. 2 Uri.getPath is :/document/image:18838  
  3. 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?
  1. Uri is:content://media/external/images/media/18822
  2. 2 Uri.getPath is :/external/images/media/18822  
  3. 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?
  1. Uri is:content://media/external/images/media/14046
  2. 2 Uri.getPath is :/external/images/media/14046  
  3. 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?
  1. //版本比較:是否是4.4及以上版本
  2.     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?
  1. /**  
  2.      * <br>功能簡述:4.4及以上從相簿選擇照片 
  3.      * <br>功能詳細描述: 
  4.      * <br>注意: 
  5.      */
  6.     @TargetApi(Build.VERSION_CODES.KITKAT)  
  7.     privatevoid SelectImageUriAfterKikat() {  
  8.         Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);  
  9.         intent.addCategory(Intent.CATEGORY_OPENABLE);  
  10.         intent.setType(”image/*”);  
  11.         startActivityForResult(intent, SELECET_A_PICTURE_AFTER_KIKAT);  
  12.     }  
/** 
     * <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?
  1. / 
  2.       <br>功能簡述:4.4以下從相簿選照片並剪下 
  3.       <br>功能詳細描述: 
  4.       <br>注意: 
  5.      /
  6.     privatevoid cropImageUri() {  
  7.         Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);  
  8.         intent.setType(”image/*”);  
  9.         intent.putExtra(”crop”“true”);  
  10.         intent.putExtra(”aspectX”, 1);  
  11.         intent.putExtra(”aspectY”, 1);  
  12.         intent.putExtra(”outputX”, 640);  
  13.         intent.putExtra(”outputY”, 640);  
  14.         intent.putExtra(”scale”true);  
  15.         intent.putExtra(”return-data”false);  
  16.         intent.putExtra(MediaStore.EXTRA_OUTPUT,  
  17.                 Uri.fromFile(new File(IMGPATH, TMP_IMAGE_FILE_NAME)));  
  18.         intent.putExtra(”outputFormat”, Bitmap.CompressFormat.JPEG.toString());  
  19.         intent.putExtra(”noFaceDetection”true); // no face detection
  20.         startActivityForResult(intent, SELECT_A_PICTURE);  
  21.     }  
/  
* <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?
  1. / 
  2.       <br>功能簡述: 4.4及以上選取照片後剪下方法 
  3.       <br>功能詳細描述: 
  4.       <br>注意: 
  5.       @param uri 
  6.      /
  7.     privatevoid cropImageUriAfterKikat(Uri uri) {  
  8.         Intent intent = new Intent(“com.android.camera.action.CROP”);  
  9.         intent.setDataAndType(uri, ”image/”);  
  10.         intent.putExtra(”crop”“true”);  
  11.         intent.putExtra(”aspectX”, 1);  
  12.         intent.putExtra(”aspectY”, 1);  
  13.         intent.putExtra(”outputX”, 640);  
  14.         intent.putExtra(”outputY”, 640);  
  15.         intent.putExtra(”scale”true);  
  16.         intent.putExtra(”return-data”true); //返回資料bitmap
  17.         intent.putExtra(”outputFormat”, Bitmap.CompressFormat.JPEG.toString());  
  18.         intent.putExtra(”noFaceDetection”true); // no face detection
  19.         startActivityForResult(intent, SET_ALBUM_PICTURE_KITKAT);  
  20.     }  
/  
* <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?
  1. / 
  2.       <br>功能簡述:對拍照的圖片剪下 
  3.       <br>功能詳細描述: 
  4.       <br>注意: 
  5.       @param uri 
  6.      */
  7.     privatevoid cameraCropImageUri(Uri uri) {  
  8.         Intent intent = new Intent(“com.android.camera.action.CROP”);  
  9.         intent.setDataAndType(uri, ”image/jpeg”);  
  10.         intent.putExtra(”crop”“true”);  
  11.         intent.putExtra(”aspectX”, 1);  
  12.         intent.putExtra(”aspectY”, 1);  
  13.         intent.putExtra(”outputX”, 640);  
  14.         intent.putExtra(”outputY”, 640);  
  15.         intent.putExtra(”scale”true);  
  16.         if (mIsKitKat) {  
  17.             intent.putExtra(”return-data”true);  
  18.         } else {  
  19.             intent.putExtra(”return-data”false);  
  20.             intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);  
  21.         }  
  22.         intent.putExtra(”outputFormat”, Bitmap.CompressFormat.JPEG.toString());  
  23.         intent.putExtra(”noFaceDetection”true);  
  24.         startActivityForResult(intent, SET_PICTURE);  
  25.     }  
/  
* <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?
  1. @Override  
  2.     protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {  
  3.         super.onActivityResult(requestCode, resultCode, data);  
  4.         if (requestCode == SELECT_A_PICTURE) {  
  5.             if (resultCode == RESULT_OK && null != data) {  
  6.                 Log.i(”zou”“4.4以下的”);  
  7.                 Bitmap bitmap = decodeUriAsBitmap(Uri.fromFile(new File(IMGPATH,  
  8.                         TMP_IMAGE_FILE_NAME)));  
  9.                 mAcountHeadIcon.setImageBitmap(bitmap);  
  10.             }  
  11.         } elseif (requestCode == SELECET_A_PICTURE_AFTER_KIKAT) {  
  12.             if (resultCode == RESULT_OK && null != data) {  
  13.                 Log.i(”zou”“4.4以上上的”);  
  14.                 mAlbumPicturePath = getPath(MainActivity.this, data.getData());  
  15.                 cropImageUriAfterKikat(Uri.fromFile(new File(mAlbumPicturePath)));  
  16.             }  
  17.         } elseif (requestCode == SET_ALBUM_PICTURE_KITKAT) {  
  18.             Log.i(”zou”“4.4以上上的 RESULT_OK”);  
  19.             Bitmap bitmap = data.getParcelableExtra(”data”);  
  20.             mAcountHeadIcon.setImageBitmap(bitmap);  
  21.         } elseif (requestCode == TAKE_A_PICTURE) {  
  22.             Log.i(”zou”“resultCode:” + resultCode);  
  23.             cameraCropImageUri(Uri.fromFile(new File(IMGPATH, IMAGE_FILE_NAME)));  
  24.         } elseif (requestCode == SET_PICTURE) {  
  25.             Log.i(”zou”“SET_PICTURE-resultCode:” + resultCode);  
  26.             Bitmap bitmap = null;  
  27.             if (mIsKitKat) {  
  28.                 if (null != data) {  
  29.                     bitmap = data.getParcelableExtra(”data”);  
  30.                 }  
  31.             } else {  
  32.                 bitmap = decodeUriAsBitmap(Uri.fromFile(new File(IMGPATH, IMAGE_FILE_NAME)));  
  33.             }  
  34.             mAcountHeadIcon.setImageBitmap(bitmap);  
  35.         }  
  36.     }  
@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);
        }
    }