简体   繁体   English

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

[英]android pie : I cant get the file name or the file path

My code is working fine on any device .. but not working on android pie 我的代码在任何设备上都可以正常工作..但是在android pie上无法工作

I am trying to get the real path and the file name from the onActivityResult in a Fragment 我正在尝试从片段中的onActivityResult获取真实路径和文件名

and I am using the FileNameUtils from the apachi library 我正在使用apachi库中的FileNameUtils

and using this library 并使用这个库

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

but its giving me null 但它给了我空

and this is my code 这是我的代码

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();
//
//      }
    }
  }

updated .. Fixed it by the next .. 更新了..下一个..已修复

first 第一

to get the file name 获取文件名

i used this function 我用了这个功能

  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();
    }
  }

then i used this methods from this post 然后我从这篇文章中使用了这种方法

Get Real Path For Uri Android 获取Uri Android的真实路径

to get the file Path 获取文件路径

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;
  }

look at this . 看这个 。 android reference android参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM