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

前端 未结 3 1635
有刺的猬
有刺的猬 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!

    0 讨论(0)
  • 2021-02-14 19:47

    You can do the following:

    @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String fileName = getFileName(this, data.getData());
    
        yourTextView.setText(fileName);
    }
    
    
    
    public String getFileName(Context context, Uri uri) {
        if (uri != null && context != null) {
          Cursor returnCursor =
              context.getContentResolver().query(uri, null, null, null, null);
          if(returnCursor != null) {
            int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
            int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
            returnCursor.moveToFirst();
            if(nameIndex >= 0 && sizeIndex >= 0){
              Log.d("File Name : " + returnCursor.getString(nameIndex));
              Log.d("File Size : " + Long.toString(returnCursor.getLong(sizeIndex)));
              Boolean isValidFile = checkFormat(returnCursor.getString(nameIndex));
    
              if(!isValidFile){
                return returnCursor.getString(nameIndex);
              }
      }
    
    private Boolean checkOtherFileType(String filePath){
        if(!Utility.isNullOrEmpty(filePath)) {
          String filePathInLowerCase = filePath.toLowerCase();
          if(filePathInLowerCase.endsWith(".pdf")) {
            return true;
          }
        }
        return false;
      }
    

    Hope this will help you out

    0 讨论(0)
  • 2021-02-14 19:51

    use startActivityForResult(intent, 1212) at the place of startActivity(intent); and do the foll0wing in onActivityResult

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case 1212:
            if (resultCode == RESULT_OK) {
                // Get the Uri of the selected file
                Uri uri = data.getData();
                String uriString = uri.toString();
                File myFile = new File(uriString);
                String path = myFile.getAbsolutePath();
                String displayName = null;
    
                if (uriString.startsWith("content://")) {                   
                    Cursor cursor = null;
                    try {                           
                        cursor = getActivity().getContentResolver().query(uri, null, null, null, null);                         
                        if (cursor != null && cursor.moveToFirst()) {                               
                            displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                        }
                    } finally {
                        cursor.close();
                    }
                } else if (uriString.startsWith("file://")) {           
                    displayName = myFile.getName();
                }
            }
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    
    0 讨论(0)
提交回复
热议问题