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

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

    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

提交回复
热议问题