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

时间:2019-01-24 07:35:11

标签: java android filepath android-file android-9.0-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();
    }
  }

然后我在这篇文章中使用了这种方法

Get Real Path For Uri Android

获取文件路径

public static String getPath(final Context context, final Uri uri) {
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    Log.i("URI",uri+"");
    String result = uri+"";
    // DocumentProvider
    //  if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
    if (isKitKat && (result.contains("media.documents"))) {
      String[] ary = result.split("/");
      int length = ary.length;
      String imgary = ary[length-1];
      final String[] dat = imgary.split("%3A");
      final String docId = dat[1];
      final String type = dat[0];
      Uri contentUri = null;
      if ("image".equals(type)) {
        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
      } else if ("video".equals(type)) {
      } else if ("audio".equals(type)) {
      }
      final String selection = "_id=?";
      final String[] selectionArgs = new String[] {
          dat[1]
      };
      return getDataColumn(context, contentUri, selection, selectionArgs);
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
      return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
      return uri.getPath();
    }
    return null;
  }

  public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = {
        column
    };
    try {
      cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
      if (cursor != null && cursor.moveToFirst()) {
        final int column_index = cursor.getColumnIndexOrThrow(column);
        return cursor.getString(column_index);
      }
    } finally {
      if (cursor != null)
        cursor.close();
    }
    return null;
  }

1 个答案:

答案 0 :(得分:1)

看看这个。 android reference