相关疑难解决方法(0)

如何从Intent.ACTION_GET_CONTENT返回的URI中提取文件名?

我正在使用第三方文件管理器从文件系统中选择一个文件(在我的情况下为PDF).

这是我启动活动的方式:

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);
Run Code Online (Sandbox Code Playgroud)

这就是我所拥有的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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

代码片段来自Open Intents文件管理器说明,网址为: …

android

79
推荐指数
10
解决办法
8万
查看次数

标签 统计

android ×1