android選擇圖片,通過uri擷取路徑

來源:互聯網
上載者:User

標籤:androi   上下文   The   split   _id   sign   返回   download   com   

    /**     * 根據Uri擷取圖片的絕對路徑     *     * @param context 內容物件     * @param uri     圖片的Uri     * @return 如果Uri對應的圖片存在, 那麼返回該圖片的絕對路徑, 否則返回null     */    public static String getRealPathFromUri(Context context, Uri uri) {        int sdkVersion = Build.VERSION.SDK_INT;        if (sdkVersion >= 19) { // api >= 19            return getRealPathFromUriAboveApi19(context, uri);        } else { // api < 19            return getRealPathFromUriBelowAPI19(context, uri);        }    }    /**     * 適配api19以下(不包括api19),根據uri擷取圖片的絕對路徑     *     * @param context 內容物件     * @param uri     圖片的Uri     * @return 如果Uri對應的圖片存在, 那麼返回該圖片的絕對路徑, 否則返回null     */    private static String getRealPathFromUriBelowAPI19(Context context, Uri uri) {        return getDataColumn(context, uri, null, null);    }    /**     * 適配api19及以上,根據uri擷取圖片的絕對路徑     *     * @param context 內容物件     * @param uri     圖片的Uri     * @return 如果Uri對應的圖片存在, 那麼返回該圖片的絕對路徑, 否則返回null     */    @SuppressLint("NewApi")    private static String getRealPathFromUriAboveApi19(Context context, Uri uri) {        String filePath = null;        if (DocumentsContract.isDocumentUri(context, uri)) {            // 如果是document類型的 uri, 則通過document id來進行處理            String documentId = DocumentsContract.getDocumentId(uri);            if (isMediaDocument(uri)) { // MediaProvider                // 使用‘:‘分割                String id = documentId.split(":")[1];                String selection = MediaStore.Images.Media._ID + "=?";                String[] selectionArgs = {id};                filePath = getDataColumn(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs);            } else if (isDownloadsDocument(uri)) { // DownloadsProvider                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId));                filePath = getDataColumn(context, contentUri, null, null);            }        } else if ("content".equalsIgnoreCase(uri.getScheme())){            // 如果是 content 類型的 Uri            filePath = getDataColumn(context, uri, null, null);        } else if ("file".equals(uri.getScheme())) {            // 如果是 file 類型的 Uri,直接擷取圖片對應的路徑            filePath = uri.getPath();        }        return filePath;    }    /**     * 擷取資料庫表中的 _data 列,即返回Uri對應的檔案路徑     * @return     */    private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {        String path = null;        String[] projection = new String[]{MediaStore.Images.Media.DATA};        Cursor cursor = null;        try {            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);            if (cursor != null && cursor.moveToFirst()) {                int columnIndex = cursor.getColumnIndexOrThrow(projection[0]);                path = cursor.getString(columnIndex);            }        } catch (Exception e) {            if (cursor != null) {                cursor.close();            }        }        return path;    }    /**     * @param uri the Uri to check     * @return Whether the Uri authority is MediaProvider     */    private 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 DownloadsProvider     */    private static boolean isDownloadsDocument(Uri uri) {        return "com.android.providers.downloads.documents".equals(uri.getAuthority());    }
  • android 4.4以上跟android4.4以前的版本通過uri擷取路徑方法不一樣,所以代碼做了處理
  • android5.1.1 一個圖片的uri→content://com.android.providers.media.documents/document/image:68

android選擇圖片,通過uri擷取路徑

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.