Android:获取文件名和任何文件的扩展名 [英] Android: Get file name and extension of any file

查看:65
本文介绍了Android:获取文件名和任何文件的扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是,我无法从使用Android 9的智能手机上选择的文件中提取文件扩展名.该问题在装有Android 8或更低版本的设备上不会出现.

The issue I have, is that i can't extract the file exetension from files choosen on a smartphone with Android 9. The problem doesn't appear on devices with Android 8 or less.

当用户选择任何类型的文件时,都会调用onActivityResult.在这种方法中,我从FileUtils类调用getPath,因此可以提取文件扩展名.

When the user has choosen any kind of file the onActivityResult gets called. In this method I call getPath from FileUtils class, so I can extract the file extension.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri uri = new Uri.Builder().build();
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case PICKFILE_REQUEST_CODE: {
                String path = new File(data.getData().getPath()).getAbsolutePath();

                if (path != null) {
                    uri = data.getData();
                    path = FileUtils.getPath(getApplicationContext().getContentResolver(), uri);
                    name = path.substring(path.lastIndexOf("."));
                    extension = path.substring(path.lastIndexOf(".") + 1);
                }
                break;
            }
        }
    }

来自FileUtils类(

getPath() from FileUtils class (from this repo) evaluates the file via URI which kind of file it is, but responsible for that case is following part:

public static String getPath(final ContentResolver cr, final Uri uri) {
    if (isDownloadsDocument(uri)) {
        final String id = DocumentsContract.getDocumentId(uri);

        final List<Uri> uris = new ArrayList<>();
        uris.add(ContentUris.withAppendedId(
                 Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)));
        uris.add(ContentUris.withAppendedId(
                 Uri.parse("content://downloads/my_downloads"), Long.valueOf(id)));
        uris.add(ContentUris.withAppendedId(
                 Uri.parse("content://downloads/all_downloads"), Long.valueOf(id)));

        String res;
        for (int i = 0; i < uris.size(); i++) {
            res = getDataColumn(cr, uris.get(i), null, null);
            if (res != null) return res;
        }
    }
}

getDataColumn()查询保存文件物理路径的文件列:

getDataColumn() queries the column of the file in which the physical path of the file is saved:

public static String getDataColumn(ContentResolver cr,
                                   Uri uri, String selection,
                                   String[] selectionArgs) {
    Cursor cursor = null;
    final String[] projection = {"_data"};
    String col = "";

    try {
        cursor = cr.query(uri, projection, selection, selectionArgs,null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow("_data");
            col = cursor.getString(column_index);
        }
    }finally{ if (cursor != null) cursor.close();}
    return col;
}

当我对其进行测试时,我得到的路径如下所示: E/AddItemActivity:结果,路径:/storage/emulated/0/Download/SampleVideo_176x144_1mb.3gp .但是,对于Android 9,该路径始终为空.

When I'm testing the it the path I get looks for example like: E/AddItemActivity: result, path: /storage/emulated/0/Download/SampleVideo_176x144_1mb.3gp. But with Android 9 the path is always empty.

推荐答案

对于该解决方案,我正在使用MediaStore提取所选文件的名称和扩展名.因此,我将 onActivityResult 中的代码替换为以下代码:

For the solution I am using the MediaStore to extract the name and extension of a choosen file. So I replace code in onActivityResult with following code:

String path = new File(data.getData().getPath()).getAbsolutePath();

if(path != null){
  uri = data.getData();

  String filename;
  Cursor cursor = getContentResolver().query(uri,null,null,null,null);
  
  if(cursor == null) filename=uri.getPath();
  else{
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Files.FileColumns.DISPLAY_NAME);
    filename = cursor.getString(idx);
    cursor.close();
  }

  String name = filename.substring(0,filename.lastIndexOf("."));
  String extension = filename.substring(filename.lastIndexOf(".")+1);
}

因此,通过 MediaStore.File.FileColumns.DISPLAY_NAME ,可以从使用文件选择器选择的任何文件中获取文件名和扩展名.

So with MediaStore.File.FileColumns.DISPLAY_NAME it's possible to get the file name and extension from any file choosen with a file chooser.

这篇关于Android:获取文件名和任何文件的扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆