ContentResolver - how to get file name from Uri

后端 未结 7 533
误落风尘
误落风尘 2020-12-30 03:38

I call startActivityForResult with Intent ACTION_GET_CONTENT. Some app returns me data with this Uri:

content://media/external/images/media/18122

I

7条回答
  •  [愿得一人]
    2020-12-30 03:57

    The accepted answer is not complete. There are more checks missed out.

    Here is what I have arrived at after a read of all the answers presented here as well what some Airgram has done in their SDKs - A utility that I have open sourced on Github:

    https://github.com/mankum93/UriUtilsAndroid/tree/master/app/src/main/java/com/androiduriutils

    Usage

    As simple as calling, UriUtils.getDisplayNameSize(). It provides both the name and size of the content.

    Note: Only works with a content:// Uri

    Here is a glimpse on the code:

    /**
     * References:
     * - https://www.programcreek.com/java-api-examples/?code=MLNO/airgram/airgram-master/TMessagesProj/src/main/java/ir/hamzad/telegram/MediaController.java
     * - https://stackoverflow.com/questions/5568874/how-to-extract-the-file-name-from-uri-returned-from-intent-action-get-content
     *
     * @author Manish@bit.ly/2HjxA0C
     * Created on: 03-07-2020
     */
    public final class UriUtils {
    
    
        public static final int CONTENT_SIZE_INVALID = -1;
    
        /**
         * @param context context
         * @param contentUri content Uri, i.e, of the scheme content://
         * @return The Display name and size for content. In case of non-determination, display name
         * would be null and content size would be {@link #CONTENT_SIZE_INVALID}
         */
        @NonNull
        public static DisplayNameAndSize getDisplayNameSize(@NonNull Context context, @NonNull Uri contentUri){
    
            final String scheme = contentUri.getScheme();
            if(scheme == null || !scheme.equals(ContentResolver.SCHEME_CONTENT)){
                throw new RuntimeException("Only scheme content:// is accepted");
            }
    
            final DisplayNameAndSize displayNameAndSize = new DisplayNameAndSize();
            displayNameAndSize.size = CONTENT_SIZE_INVALID;
    
            String[] projection = new String[]{MediaStore.Images.Media.DATA, OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE};
            Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
            try {
                if (cursor != null && cursor.moveToFirst()) {
    
                    // Try extracting content size
    
                    int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
                    if (sizeIndex != -1) {
                        displayNameAndSize.size = cursor.getLong(sizeIndex);
                    }
    
                    // Try extracting display name
                    String name = null;
    
                    // Strategy: The column name is NOT guaranteed to be indexed by DISPLAY_NAME
                    // so, we try two methods
                    int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                    if (nameIndex != -1) {
                        name = cursor.getString(nameIndex);
                    }
    
                    if (nameIndex == -1 || name == null) {
                        nameIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                        if (nameIndex != -1) {
                            name = cursor.getString(nameIndex);
                        }
                    }
                    displayNameAndSize.displayName = name;
                }
            }
            finally {
                if(cursor != null){
                    cursor.close();
                }
            }
    
            // We tried querying the ContentResolver...didn't work out
            // Try extracting the last path segment
            if(displayNameAndSize.displayName == null){
                displayNameAndSize.displayName = contentUri.getLastPathSegment();
            }
    
            return displayNameAndSize;
        }
    }
    

提交回复
热议问题