猿问

android pie:我无法获取文件名或文件路径

我的代码在任何设备上都可以正常工作..但不能在 android pie 上工作


我正在尝试从片段中的 onActivityResult 获取真实路径和文件名


我正在使用 apachi 库中的 FileNameUtils


并使用这个库


https://gist.github.com/tatocaster/32aad15f6e0c50311626


但它给了我空


这是我的代码


private void pickFile() {


    Intent intent = new Intent();

    intent.setType("*/*");

    intent.setAction(Intent.ACTION_GET_CONTENT);

    startActivityForResult(Intent.createChooser(intent, "Select File"), PICKFILE_REQUEST_CODE);


  }



  @Override

  public void onActivityResult(int requestCode, int resultCode, Intent data) {


    if (requestCode == PICKFILE_REQUEST_CODE) {


      filePath = RealPathUtil.getRealPath(getContext(), data.getData());

      fileName = FilenameUtils.getName(filePath);


      Log.i("FileNameIs",filePath + "Hello  " + fileName );


//      if (fileName !=null)

//      {

//        mFileName

//            .setText(fileName.isEmpty() ? getString(R.string.failed_please_try_again) : fileName);

//

//        deleteOldPath();

//

//      }

    }

  }

更新..在下一个..修复它


第一的


获取文件名


我用过这个功能


  public void dumpImageMetaData(Uri uri) {


    String TAG = "TagIs";

    // The query, since it only applies to a single document, will only return

    // one row. There's no need to filter, sort, or select fields, since we want

    // all fields for one document.

    Cursor cursor = getActivity().getContentResolver()

        .query(uri, null, null, null, null, null);


    try {

      // moveToFirst() returns false if the cursor has 0 rows.  Very handy for

      // "if there's anything to look at, look at it" conditionals.

      if (cursor != null && cursor.moveToFirst()) {


        // Note it's called "Display Name".  This is

        // provider-specific, and might not necessarily be the file name.

        String displayName = cursor.getString(

            cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

        fileName = displayName ==null ? "Failed" : displayName;


        deleteOldPath();


      }

    } finally {

      cursor.close();

    }

  }


斯蒂芬大帝
浏览 330回答 1
1回答

梦里花落0921

看这个 。安卓参考
随时随地看视频慕课网APP

相关分类

Java
我要回答