Select pdf file from phone on button click and display its file name on textview

前端 未结 3 1657
有刺的猬
有刺的猬 2021-02-14 19:30

I want to select pdf file from phone on button click and display its file name on a text view. till now I have done opening file manager for selecting pdf on button click

<
3条回答
  •  南笙
    南笙 (楼主)
    2021-02-14 19:47

    Add in your getPDFPath method:

    public String getPDFPath(Uri uri){
    
         final String id = DocumentsContract.getDocumentId(uri);
         final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
    
         String[] projection = { MediaStore.Images.Media.DATA };
         Cursor cursor = getContentResolver().query(contentUri, projection, null, null, null);
         int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
         cursor.moveToFirst();
         return cursor.getString(column_index);
    }
    

    In your case, this code is specifically for documents from DonwloadProvider, for further implementation check Paul Burke answer. I personally use his aFileChooser library to avoid this kind of problems.

    Hope this helps!

提交回复
热议问题