如何让用户在 Android 中使用 Intent 仅选择本地文件? [英] How to let user select only local files using Intent in Android?

查看:54
本文介绍了如何让用户在 Android 中使用 Intent 仅选择本地文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要让用户从他们的本地存储中为我的应用程序选择一个文件.现在我正在使用 INTENT.ACTION_GET_CONTENT 让用户选择文件,但它也提供了从云中选择 URI 的选项.获取URI文件后,我将其视为本地文件并执行各种操作(包括提取文件扩展名).如何让用户只选择本地文件?

I need to let the user select a file from their local storage for my application. Right now I'm using INTENT.ACTION_GET_CONTENT to let the user select the file, but it also gives the option of selecting URI from the cloud. After I get the URI file, I treat it as a local file and perform various things (including extracting the file extension). How can I let the user only pick local files?

else if(menuItem.getItemId() == R.id.action_import_from_file){
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.setType("*/*");
    startActivityForResult(Intent.createChooser(i, "Pick a file"), REQUEST_CODE_SELECT_FILE_FOR_IMPORT);
}

推荐答案

请尝试使用 Intent.EXTRA_LOCAL_ONLY 喜欢
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
如果您设备中的图库应用支持,则只会显示本地存储中的文件.如果图库应用不支持上述意图,您可以在检查图库应用返回的文件路径的文件长度后向用户显示错误消息.

Please try using Intent.EXTRA_LOCAL_ONLY like
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
If the gallery app in your device supports it, only files in local storage will be displayed. If the gallery app does not support the above intent, you can show an error message to the user after checking the file length of the returned file path from gallery app.

这篇关于如何让用户在 Android 中使用 Intent 仅选择本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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