Java Code Examples for android.provider.DocumentsContract#getDocumentId()

The following examples show how to use android.provider.DocumentsContract#getDocumentId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileUtils.java    From HeartBeat with Apache License 2.0 9 votes vote down vote up
@SuppressLint("NewApi")
public static String uriToPath(Context context, Uri uri) {
    boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        final String docId = DocumentsContract.getDocumentId(uri);
        Log.d("maxiee", "docID:" + docId);
        final String[] split = docId.split(":");
        final String type = split[0];
        Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        final String selection = "_id=?";
        final String[] selectionArgs = new String[] {split[1]};
        return getDataColumn(context, contentUri, selection, selectionArgs);
    } else if ("content".equalsIgnoreCase(uri.getScheme())){
        return getDataColumn(context, uri, null, null);
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    return null;
}
 
Example 2
Source File: SettingPresenterImpel.java    From BetterWay with Apache License 2.0 8 votes vote down vote up
@Override
    public String handleImageOnKitKat(Intent data) {
        LogUtil.d(TAG, "handleImageOnkitKat");
        String imagePath = null;
        Uri uri = data.getData();
        if (DocumentsContract.isDocumentUri(mSettingView.getSettingActivity().getApplicationContext(), uri)) {
        //如果是document类型的Uri,则通过documentid处理
        String docId = DocumentsContract.getDocumentId(uri);
        if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
            String id = docId.split(":")[1]; //解析出数字格式的id
            String selection = MediaStore.Images.Media._ID + '=' + id;
            imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
        } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
            Uri contenUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(docId));
            imagePath = getImagePath(contenUri, null);
        }
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
            imagePath = getImagePath(uri, null);
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            imagePath = uri.getPath();
    }
        return imagePath;
}
 
Example 3
Source File: DocumentsContractApi21.java    From UniFile with Apache License 2.0 7 votes vote down vote up
public static Uri prepareTreeUri(Uri treeUri) {
    String documentId;
    try {
        documentId = DocumentsContract.getDocumentId(treeUri);
        if (documentId == null) {
            throw new IllegalArgumentException();
        }
    } catch (Exception e) {
        // IllegalArgumentException will be raised
        // if DocumentsContract.getDocumentId() failed.
        // But it isn't mentioned the document,
        // catch all kinds of Exception for safety.
        documentId = DocumentsContract.getTreeDocumentId(treeUri);
    }
    return DocumentsContract.buildDocumentUriUsingTree(treeUri, documentId);
}
 
Example 4
Source File: FilePathHelper.java    From qcloud-sdk-android-samples with MIT License 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private static String getKitKatPathFromMediaUri(Context context, Uri uri) {

    String imagePath = "";
    if (DocumentsContract.isDocumentUri(context, uri)) {
        String docId = DocumentsContract.getDocumentId(uri);
        if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
            //Log.d(TAG, uri.toString());
            String id = docId.split(":")[1];
            String selection = MediaStore.Images.Media._ID + "=" + id;

            imagePath = getImagePathFromMediaUri(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
        } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
            //Log.d(TAG, uri.toString());
            Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(docId));
            imagePath = getImagePathFromMediaUri(context, contentUri, null);
        }
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
        //Log.d(TAG, "content: " + uri.toString());
        imagePath = getImagePathFromMediaUri(context, uri, null);
    }
    return imagePath;
}
 
Example 5
Source File: SelectAlbumUtils.java    From imsdk-android with MIT License 6 votes vote down vote up
/**
 * 4.4以上的处理
 *
 * @param data
 * @return
 */
@TargetApi(19)
private static String getPicOnKitKatAfter(Context context, Intent data) {
    String imagePath = null;
    Uri uri = data.getData();
    if (DocumentsContract.isDocumentUri(context, uri)) {
        String docId = DocumentsContract.getDocumentId(uri);
        if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
            String id = docId.split(":")[1];
            String selection = MediaStore.Images.Media._ID + "=" + id;
            imagePath = getImagePath(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
        } else if ("com.android.provider.downloads.documents".equals(uri.getAuthority())) {
            Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
            imagePath = getImagePath(context, contentUri, null);
        }
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
        imagePath = getImagePath(context, uri, null);
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        imagePath = uri.getPath();
    }
    return imagePath;
}
 
Example 6
Source File: MainActivity.java    From GenerateQRCode with Apache License 2.0 6 votes vote down vote up
/**
 * 4.4以后
 *
 * @param data
 */
@SuppressLint("NewApi")
private void handleImageOnKitKat(Intent data) {
    String imagePath = null;
    Uri uri = data.getData();
    Log.d("TAG", "handleImageOnKitKat: uri is " + uri);
    if (DocumentsContract.isDocumentUri(this, uri)) {
        // 如果是document类型的Uri,则通过document id处理
        String docId = DocumentsContract.getDocumentId(uri);
        if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
            String id = docId.split(":")[1]; // 解析出数字格式的id
            String selection = MediaStore.Images.Media._ID + "=" + id;
            imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
        } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
            Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
            imagePath = getImagePath(contentUri, null);
        }
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
        // 如果是content类型的Uri,则使用普通方式处理
        imagePath = getImagePath(uri, null);
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        // 如果是file类型的Uri,直接获取图片路径即可
        imagePath = uri.getPath();
    }
    displayImage(imagePath); // 根据图片路径显示图片
}
 
Example 7
Source File: FileHelper.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
@Nullable public static String getPath(@NonNull Context context, @NonNull Uri uri) {
    String filePath = null;
    try {
        String wholeID = DocumentsContract.getDocumentId(uri);
        String id = wholeID.split(":")[1];
        String[] column = {MediaStore.Images.Media.DATA};
        String sel = MediaStore.Images.Media._ID + "=?";
        try (Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                column, sel, new String[]{id}, null)) {
            if (cursor != null) {
                int columnIndex = cursor.getColumnIndex(column[0]);
                if (cursor.moveToFirst()) {
                    filePath = cursor.getString(columnIndex);
                }
            }
        }
    } catch (Exception ignored) {}
    return filePath;
}
 
Example 8
Source File: UriUtils.java    From YZxing with Apache License 2.0 6 votes vote down vote up
private static String getPicturePathFromUriAboveApi19(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;
}
 
Example 9
Source File: CameraUtils.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@TargetApi(19)
public static void handleImageOnKitKat(Activity activity, Intent data) {
    Uri uri = data.getData();
    if (DocumentsContract.isDocumentUri(activity, uri)) {
        // 如果是document类型的Uri,则通过document id进行处理
        String docId = DocumentsContract.getDocumentId(uri);
        if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
            String id = docId.split(":")[1]; // 解析出数字格式的id
            String selection = MediaStore.Images.Media._ID + "=" + id;
            albumPhotonUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        } else if ("com.android.provides.downloads.documents".equals(uri.getAuthority())) {
            Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(docId));
            albumPhotonUri = contentUri;
        }
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
        // 如果不是document类型的uri,则使用普通方式处理
        albumPhotonUri = uri;
    }

    albumChooseZoom(activity, uri);
}
 
Example 10
Source File: FileUtils.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
static String getPath(final Context context, final Uri uri) {
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    if (isKitKat && isMediaDocumentAuthority(uri)) {
        final String documentId = DocumentsContract.getDocumentId(uri); // e.g. "image:1234"
        final String[] parts = documentId.split(":");
        final String type = parts[0];

        final Uri contentUri;
        if ("image".equals(type)) {
            contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        } else {
            // reject video or audio documents
            return null;
        }

        // query content resolver for MediaStore id column
        final String selection = "_id=?";
        final String[] args = new String[] {
                parts[1]
        };
        return resolveFilePath(context, contentUri, selection, args);
    } else if (isContentScheme(uri)) {
        return resolveFilePath(context, uri, null, null);
    } else if (isFileScheme(uri)) {
        return uri.getPath();
    }
    return null;
}
 
Example 11
Source File: UriUtils.java    From chat-window-android with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getFilePathForDownloadsDocumentUri(Context context, Uri uri) {
    String documentId = DocumentsContract.getDocumentId(uri);

    Uri downloadsContentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId));

    return getDataColumnForContentUri(context, downloadsContentUri, null, null);
}
 
Example 12
Source File: UriUtils.java    From chat-window-android with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getFilePathForExternalStorageDocumentUri(Uri uri) {
    String documentId = DocumentsContract.getDocumentId(uri);

    String[] split = documentId.split(":");

    String uriContentType = split[0];
    String uriId = split[1];

    if ("primary".equalsIgnoreCase(uriContentType)) {
        return Environment.getExternalStorageDirectory() + "/" + uriId;
    } else {
        return uri.getPath();
    }
}
 
Example 13
Source File: PathUtil.java    From TextThing with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@SuppressLint("NewApi")
public static String getPath(Context context, Uri uri) throws URISyntaxException {
    final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19;
    String selection = null;
    String[] selectionArgs = null;
    // Uri is different in versions after KITKAT (Android 4.4), we need to
    // deal with different Uris.
    if (needToCheckUri && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");

            Log.d(App.PACKAGE_NAME, "uri get path from: " + uri.toString());
            if (uri.toString().startsWith("content://com.android.externalstorage.documents/document/home%3A")) {
                return Environment.getExternalStorageDirectory() + "/Documents/" + split[1];
            } else {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }
        } else if (isDownloadsDocument(uri)) {
            Log.w(App.PACKAGE_NAME, "files from magic downloads folder not supported");

            /*
            final String id = DocumentsContract.getDocumentId(uri);
            uri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
            */
        } else if (isMediaDocument(uri)) {
            Log.w(App.PACKAGE_NAME, "files from magic media folder not supported");
            /*
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            if ("image".equals(type)) {
                uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }
            selection = "_id=?";
            selectionArgs = new String[]{ split[1] };
            */
        }
    }

    return uri.getPath();
}
 
Example 14
Source File: CursorUtils.java    From hipda with GNU General Public License v2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private static ImageFileInfo getImageInfo_API19(Context context, Uri uri) {
    ImageFileInfo result = new ImageFileInfo();
    String[] column = {MediaStore.Images.Media.DATA, MediaStore.Images.ImageColumns.ORIENTATION};

    Cursor cursor = null;
    try {
        String wholeID = DocumentsContract.getDocumentId(uri);
        String id = wholeID.split(":")[1];
        String sel = MediaStore.Images.Media._ID + "=?";

        cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                column, sel, new String[]{id}, null);
        int pathIndex = cursor.getColumnIndex(column[0]);
        int orientationIndex = cursor.getColumnIndex(column[1]);

        if (cursor.moveToFirst()) {
            if (pathIndex >= 0)
                result.setFilePath(cursor.getString(pathIndex));
            if (orientationIndex >= 0)
                result.setOrientation(cursor.getInt(orientationIndex));
        }
    } catch (Exception e) {
        Logger.e(e);
        return null;
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return result;
}
 
Example 15
Source File: FileUtil.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
/**
 * WARNING This is a tweak of internal Android code to make it faster by caching calls to queryIntentContentProviders
 * Original (uncached) is DocumentFile.fromTreeUri
 */
@Nullable
private static DocumentFile fromTreeUriCached(@NonNull final Context context, @NonNull final Uri treeUri) {
    String documentId = DocumentsContract.getTreeDocumentId(treeUri);
    if (isDocumentUriCached(context, treeUri)) {
        documentId = DocumentsContract.getDocumentId(treeUri);
    }
    return newTreeDocumentFile(null, context,
            DocumentsContract.buildDocumentUriUsingTree(treeUri,
                    documentId));
}
 
Example 16
Source File: FileHelper.java    From reader with MIT License 5 votes vote down vote up
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
    String filePath = "";
    try {
        String wholeID = DocumentsContract.getDocumentId(uri);

        // Split at colon, use second item in the array
        String id = wholeID.indexOf(":") > -1 ? wholeID.split(":")[1] : wholeID.indexOf(";") > -1 ? wholeID
                .split(";")[1] : wholeID;

        String[] column = { MediaStore.Images.Media.DATA };

        // where id is equal to
        String sel = MediaStore.Images.Media._ID + "=?";

        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
                sel, new String[] { id }, null);

        int columnIndex = cursor.getColumnIndex(column[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();
    } catch (Exception e) {
        filePath = "";
    }
    return filePath;
}
 
Example 17
Source File: UriUtil.java    From vmqApk with MIT License 5 votes vote down vote up
/**
 * 适配api19以上,根据uri获取图片的绝对路径
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getRealPathFromUri_AboveApi19(Context context, Uri uri) {
    String filePath = null;
    String wholeID = DocumentsContract.getDocumentId(uri);

    // 使用':'分割
    String[] ids = wholeID.split(":");
    String id = null;
    if (ids == null) {
        return null;
    }
    if (ids.length > 1) {
        id = ids[1];
    } else {
        id = ids[0];
    }

    String[] projection = {MediaStore.Images.Media.DATA};
    String selection = MediaStore.Images.Media._ID + "=?";
    String[] selectionArgs = {id};

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,//
            projection, selection, selectionArgs, null);
    int columnIndex = cursor.getColumnIndex(projection[0]);
    if (cursor.moveToFirst()) filePath = cursor.getString(columnIndex);
    cursor.close();
    return filePath;
}
 
Example 18
Source File: OcrUtils.java    From LockDemo with Apache License 2.0 4 votes vote down vote up
public static String getPath(Context context,
                             Uri uri) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if (DocumentsContract.isDocumentUri(context, uri)) {
            String docId;
            String[] split;
            String type;
            if (isExternalStorageDocument(uri)) {
                docId = DocumentsContract.getDocumentId(uri);
                split = docId.split(":");
                type = split[0];
                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }
            } else {
                if (isDownloadsDocument(uri)) {
                    docId = DocumentsContract.getDocumentId(uri);
                    Uri split1 = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                            Long.valueOf(docId));
                    return getDataColumn(context,
                            split1,
                            null,
                            null);
                }

                if (isMediaDocument(uri)) {
                    docId = DocumentsContract.getDocumentId(uri);
                    split = docId.split(":");
                    type = split[0];
                    Uri contentUri = null;
                    if ("image".equals(type)) {
                        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    } else if ("video".equals(type)) {
                        contentUri = android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                    } else if ("audio".equals(type)) {
                        contentUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                    }

                    String selection = "_id=?";
                    String[] selectionArgs = new String[]{split[1]};
                    return getDataColumn(context,
                            contentUri,
                            "_id=?",
                            selectionArgs);
                }
            }
        }
    }

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        if (isGooglePhotosUri(uri)) {
            return uri.getLastPathSegment();
        }

        return getDataColumn(context,
                uri,
                null,
                null);
    }

    if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }


    return null;
}
 
Example 19
Source File: ConvertUtils.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 从第三方文件选择器获取路径。
 * 参见:http://blog.csdn.net/zbjdsbj/article/details/42387551
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String toPath(Context context, Uri uri) {
    if (uri == null) {
        return "";
    }
    String path = uri.getPath();
    String scheme = uri.getScheme();
    String authority = uri.getAuthority();
    //是否是4.4及以上版本
    boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        String docId = DocumentsContract.getDocumentId(uri);
        String[] split = docId.split(":");
        String type = split[0];
        Uri contentUri = null;
        switch (authority) {
            // ExternalStorageProvider
            case "com.android.externalstorage.documents":
                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }
                break;
            // DownloadsProvider
            case "com.android.providers.downloads.documents":
                contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
                return _queryPathFromMediaStore(context, contentUri, null, null);
            // MediaProvider
            case "com.android.providers.media.documents":
                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;
                }
                String selection = "_id=?";
                String[] selectionArgs = new String[]{split[1]};
                return _queryPathFromMediaStore(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else {
        if ("content".equalsIgnoreCase(scheme)) {
            // Return the remote address
            if (authority.equals("com.google.android.apps.photos.content")) {
                return uri.getLastPathSegment();
            }
            return _queryPathFromMediaStore(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(scheme)) {
            return uri.getPath();
        }
    }
    return path;
}
 
Example 20
Source File: CompatibilityImpl.java    From Overchan-Android with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getDocumentId(Uri uri) {
    return DocumentsContract.getDocumentId(uri);
}