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

后端 未结 17 1995
梦如初夏
梦如初夏 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: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;
    }
    

提交回复
热议问题