如何提取URI从Intent.ACTION_GET_CONTENT返回的文件名? [英] How to extract the file name from URI returned from Intent.ACTION_GET_CONTENT?

查看:1445
本文介绍了如何提取URI从Intent.ACTION_GET_CONTENT返回的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的第三方文件管理器选择从文件系统中的文件(PDF在我的情况)。

这是我发起活动:

 意向意图=新的意图(Intent.ACTION_GET_CONTENT);
intent.setType(的getString(R.string.app_pdf_mime_type));
intent.addCategory(Intent.CATEGORY_OPENABLE);

字符串chooserName =的getString(R.string.Browse);
意向选择器= Intent.createChooser(意向,chooserName);

startActivityForResult(选配,ActivityRequests.BROWSE);
 

这是我在 onActivityResult

 开放的URI = data.getData();
如果(URI!= NULL){
    如果(uri.toString()startsWith(文件)){
        文件名= uri.getPath();
    }其他{// uri.startsWith(内容)

        光标C = getContentResolver()查询(URI,NULL,NULL,NULL,NULL);

        如果(C =空&安培;!&安培; c.moveToFirst()){

            INT ID = c.getColumnIndex(Images.Media.DATA);
            如果(ID!= -1){
                文件名= c.getString(ID);
            }
        }
    }
}
 

code段是从的打开意图借了文件管理器的位置说明可供选择:
http://www.openintents.org/en/node/829

目的的if-else 的向后兼容性。我不知道这是获得文件名的最好方式,因为我已经发现,其他的文件管理器返回所有样的东西。

例如,文件多哥的返回类似如下:

<$p$p><$c$c>content://com.dataviz.dxtg.documentprovider/document/file%3A%2F%2F%2Fsdcard%2Fdropbox%2FTransfer%2Fconsent.pdf

getContentResolver()查询()返回

为了让事情更有趣,未命名的文件管理器(我得到这个URI的客户端日志)返回的是这样的:

  /./ SD卡/下载/ .bin文件
 


有没有从URI中提取文件名或应该采取的字符串解析的preferred方式?

解决方案

我使用的是这样的:

 字符串模式= uri.getScheme();
如果(scheme.equals(文件)){
    文件名= uri.getLastPathSegment();
}
否则,如果(scheme.equals(内容)){
    的String []凸出= {MediaStore.Images.Media.TITLE};
    光标光标= context.getContentResolver()查询(contentUri,凸出,NULL,NULL,NULL);
    如果(光标=空&安培;!&安培;!cursor.getCount()= 0){
        INT参数:columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE);
        cursor.moveToFirst();
        文件名= cursor.getString(参数:columnIndex);
    }
    如果(光标!= NULL){
        cursor.close();
    }
}
 

I am using 3rd party file manager to pick a file (PDF in my case) from the file system.

This is how I launch the activity:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(getString(R.string.app_pdf_mime_type));
intent.addCategory(Intent.CATEGORY_OPENABLE);

String chooserName = getString(R.string.Browse);
Intent chooser = Intent.createChooser(intent, chooserName);

startActivityForResult(chooser, ActivityRequests.BROWSE);

This is what I have in onActivityResult:

Uri uri = data.getData();
if (uri != null) {
    if (uri.toString().startsWith("file:")) {
        fileName = uri.getPath();
    } else { // uri.startsWith("content:")

        Cursor c = getContentResolver().query(uri, null, null, null, null);

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

            int id = c.getColumnIndex(Images.Media.DATA);
            if (id != -1) {
                fileName = c.getString(id);
            }
        }
    }
}

Code snippet is borrowed from Open Intents File Manager instructions available here:
http://www.openintents.org/en/node/829

The purpose of if-else is backwards compatibility. I wonder if this is a best way to get the file name as I have found that other file managers return all kind of things.

For example, Documents ToGo return something like the following:

content://com.dataviz.dxtg.documentprovider/document/file%3A%2F%2F%2Fsdcard%2Fdropbox%2FTransfer%2Fconsent.pdf

on which getContentResolver().query() returns null.

To make things more interesting, unnamed file manager (I got this URI from client log) returned something like:

/./sdcard/downloads/.bin


Is there a preferred way of extracting the file name from URI or one should resort to string parsing?

解决方案

I'm using something like this:

String scheme = uri.getScheme();
if (scheme.equals("file")) {
    fileName = uri.getLastPathSegment();
}
else if (scheme.equals("content")) {
    String[] proj = { MediaStore.Images.Media.TITLE };
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor != null && cursor.getCount() != 0) {
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE);
        cursor.moveToFirst();
        fileName = cursor.getString(columnIndex);
    }
    if (cursor != null) {
        cursor.close();
    }
}

这篇关于如何提取URI从Intent.ACTION_GET_CONTENT返回的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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