【问题标题】:Android: Incorrect File name for selected file(From file chooser)Android:所选文件的文件名不正确(来自文件选择器)
【发布时间】:2017-12-16 15:57:13
【问题描述】:

我有一个屏幕,单击按钮打开文件选择器,然后选择一个名为“Test.jpg”的文件以进行进一步操作。 我使用以下代码来获取该文件的名称。

Uri uri = data.getData();
File file = new File(uri.getPath());
String fileName = file.getName();

这是调试器的结果

file.getName() => 167522

file.toString() => /external/images/media/167522

我想获取 Test.jpg 作为我的文件名。 请让我知道我的代码有什么问题。

【问题讨论】:

  • 试试这个你会得到完整的文件路径和名称stackoverflow.com/a/41520090/1548824
  • 您使用的是哪个文件选择器?
  • 我正在使用安卓意图选择器
  • @AkhileshPatil 链接中提到的getPath方法给出了null

标签: java android file filechooser


【解决方案1】:

需要来自您的 uri 的路径。这是一种从 uri 获取路径的方法。

public  String getPath(Context context, Uri uri) throws URISyntaxException {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    }
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

获取文件名

try{
  //call the getPath uri with context and uri
  //To get path from uri
  String path = getPath(this, uri);
  File file = new File(path);
  String filename = file.getName();
  Log.e(TAG, "File Name: " + filename);
}catch(Exception e){
   e("Err", e.toString()+"");
}

输出

uri: content://com.android.providers.media.documents/document/image%3A12876
FileName : profile.png

【讨论】:

    【解决方案2】:

    这种方法在 Kotlin 中对我有用:

    private fun getFilename(uri: Uri): String? {
        val cursor = activity?.contentResolver?.query(uri, null, null, null, null)
        var filename: String? = null
    
        cursor?.getColumnIndex(OpenableColumns.DISPLAY_NAME)?.let { nameIndex ->
            cursor.moveToFirst()
    
            filename = cursor.getString(nameIndex)
            cursor.close()
        }
    
        return filename
    }
    

    【讨论】:

      【解决方案3】:

      尝试使用这种方法:

       public String getRealPathFromURI(Context context, Uri contentUri) {
          Cursor cursor = null;
          try {
              String[] proj = { MediaStore.Images.Media.DATA };
              cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
              int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
              cursor.moveToFirst();
              return cursor.getString(column_index);
          } finally {
              if (cursor != null) {
                  cursor.close();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-26
        • 1970-01-01
        • 2015-10-17
        • 2021-01-21
        • 1970-01-01
        相关资源
        最近更新 更多