How to extract the file name from URI returned from Intent.ACTION_GET_CONTENT?

后端 未结 17 1996
梦如初夏
梦如初夏 2020-11-28 05:09

I am using 3rd party file manager to pick a file (PDF in my case) from the file system.

This is how I launch the activity:

Intent i         


        
相关标签:
17条回答
  • 2020-11-28 05:47

    If you want it short this should work.

    Uri uri= data.getData();
    File file= new File(uri.getPath());
    file.getName();
    
    0 讨论(0)
  • 2020-11-28 05:47

    My version of the answer is actually very similar to the @Stefan Haustein. I found the answer on Android Developer page Retrieving File Information; the information here is even more condensed on this specific topic than on Storage Access Framework guide site. In the result from the query the column index containing file name is OpenableColumns.DISPLAY_NAME. None of other answers/solutions for column indexes worked for me. Below is the sample function:

     /**
     * @param uri uri of file.
     * @param contentResolver access to server app.
     * @return the name of the file.
     */
    def extractFileName(uri: Uri, contentResolver: ContentResolver): Option[String] = {
    
        var fileName: Option[String] = None
        if (uri.getScheme.equals("file")) {
    
            fileName = Option(uri.getLastPathSegment)
        } else if (uri.getScheme.equals("content")) {
    
            var cursor: Cursor = null
            try {
    
                // Query the server app to get the file's display name and size.
                cursor = contentResolver.query(uri, null, null, null, null)
    
                // Get the column indexes of the data in the Cursor,
                // move to the first row in the Cursor, get the data.
                if (cursor != null && cursor.moveToFirst()) {
    
                    val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
                    fileName = Option(cursor.getString(nameIndex))
                }
    
            } finally {
    
                if (cursor != null) {
                    cursor.close()
                }
    
            }
    
        }
    
        fileName
    }
    
    0 讨论(0)
  • I use below code to get File Name & File Size from Uri in my project.

    /**
     * Used to get file detail from uri.
     * <p>
     * 1. Used to get file detail (name & size) from uri.
     * 2. Getting file details from uri is different for different uri scheme,
     * 2.a. For "File Uri Scheme" - We will get file from uri & then get its details.
     * 2.b. For "Content Uri Scheme" - We will get the file details by querying content resolver.
     *
     * @param uri Uri.
     * @return file detail.
     */
    public static FileDetail getFileDetailFromUri(final Context context, final Uri uri) {
        FileDetail fileDetail = null;
        if (uri != null) {
            fileDetail = new FileDetail();
            // File Scheme.
            if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
                File file = new File(uri.getPath());
                fileDetail.fileName = file.getName();
                fileDetail.fileSize = file.length();
            }
            // Content Scheme.
            else if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
                Cursor returnCursor =
                        context.getContentResolver().query(uri, null, null, null, null);
                if (returnCursor != null && returnCursor.moveToFirst()) {
                    int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                    int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
                    fileDetail.fileName = returnCursor.getString(nameIndex);
                    fileDetail.fileSize = returnCursor.getLong(sizeIndex);
                    returnCursor.close();
                }
            }
        }
        return fileDetail;
    }
    
    /**
     * File Detail.
     * <p>
     * 1. Model used to hold file details.
     */
    public static class FileDetail {
    
        // fileSize.
        public String fileName;
    
        // fileSize in bytes.
        public long fileSize;
    
        /**
         * Constructor.
         */
        public FileDetail() {
    
        }
    }
    
    0 讨论(0)
  • 2020-11-28 05:51
    public String getFilename() 
    {
    /*  Intent intent = getIntent();
        String name = intent.getData().getLastPathSegment();
        return name;*/
        Uri uri=getIntent().getData();
        String fileName = null;
        Context context=getApplicationContext();
        String scheme = uri.getScheme();
        if (scheme.equals("file")) {
            fileName = uri.getLastPathSegment();
        }
        else if (scheme.equals("content")) {
            String[] proj = { MediaStore.Video.Media.TITLE };
            Uri contentUri = null;
            Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
            if (cursor != null && cursor.getCount() != 0) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE);
                cursor.moveToFirst();
                fileName = cursor.getString(columnIndex);
            }
        }
        return fileName;
    }
    
    0 讨论(0)
  • 2020-11-28 05:52

    Taken from Retrieving File information | Android developers

    Retrieving a File's name.

    private String queryName(ContentResolver resolver, Uri uri) {
        Cursor returnCursor =
                resolver.query(uri, null, null, null, null);
        assert returnCursor != null;
        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        returnCursor.moveToFirst();
        String name = returnCursor.getString(nameIndex);
        returnCursor.close();
        return name;
    }
    
    0 讨论(0)
  • 2020-11-28 05:53

    The most condensed version:

    public String getNameFromURI(Uri uri) {
        Cursor c = getContentResolver().query(uri, null, null, null, null);
        c.moveToFirst();
        return c.getString(c.getColumnIndex(OpenableColumns.DISPLAY_NAME));
    }
    
    0 讨论(0)
提交回复
热议问题