关键词:
【中文标题】FileProvider - 从下载目录打开文件【英文标题】:FileProvider - Open File from Download Directory 【发布时间】:2017-08-23 12:51:36 【问题描述】:我无法从下载文件夹中打开任何文件。
我可以用这个下载文件并保存在下载文件夹中:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(descricao);
request.setTitle(titulo);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nome);
enq = downloadManager.enqueue(request);
在此之后,我的文件正确保存在目录文件夹:Android >> 内部共享存储 >> 下载。 ***这条路径我看到在 ubuntu 中手动打开设备的 hd。如图所示路径。 Android HD by ubuntu folder - see the path
我尝试用这个打开这个文件:
downloadManager = (DownloadManager)getContext().getSystemService(DOWNLOAD_SERVICE);
BroadcastReceiver receiver = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
String action = intent.getAction();
if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action))
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(enq);
Cursor c = downloadManager.query(query);
if(c.moveToFirst())
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if(DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex))
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
if (uriString.substring(0, 7).matches("file://"))
uriString = uriString.substring(7);
File file = new File(uriString);
Uri uriFile = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
String mimetype = "application/pdf";
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setDataAndType(uriFile, mimetype);
Intent intentChooser = Intent.createChooser(myIntent, "Choose Pdf Application");
startActivity(intentChooser);
;
getContext().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
我在清单中声明我的文件提供程序:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="$applicationId.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
还有这个:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="Download" path="Download/"/>
</paths>
但是当我点击按钮下载时,我收到这条消息:“无法访问此文件。请检查位置或网络,然后重试。”
正在恢复:
1 - 文件被下载并保存在目录文件夹中。
2 - Intent 已启动,但文件未打开。
3 - 调试模式在“new File(urlString)”处给我这个:“urlString=/storage/emulated/0/Download/name.pdf”
4 - 在“FileProvider.getUriFromFile...”调试模式有这样的:
“uriFile = content://com.example.android.parlamentaresapp.fileprovider/Download/name.pdf”
谢谢。
【问题讨论】:
Android >> Internal Shared Storage >> Download.
。这不是您可以在代码中使用的文件系统路径。请提供实际的完整路径。
【参考方案1】:
在您与startActivity()
和FileProvider
一起使用的Intent
上调用addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
Uri
。否则,该活动无权访问您的内容。
【讨论】:
重要:file的文件名不能有空格,使用:“file-one.txt”而不是“file one.txt” 意图意图=新意图(意图.ACTION_GET_CONTENT);意图.addFlags(意图.FLAG_GRANT_READ_URI_PERMISSION);意图.setType("/"); startActivityForResult(意图,101);当我从图像目录中选择图像时,我使用此代码在 android 中打开文件管理器,它可以正常工作并将其转换为文件并发送到与音频和视频目录相同的服务器,但是当我从下载目录或最近的文件中选择某些内容时,我没有将其转换为文件,它会抛出异常。 @VishnuSaini;我不知道您所说的“将其转换为文件”是什么意思。而且,我确实知道例外是什么。我建议您提出一个新的 Stack Overflow 问题,在其中提供 minimal reproducible example 显示您的代码、崩溃的完整堆栈跟踪以及有关您的情况的其他详细信息。Android:使用 FileProvider 的外部存储文件的 URI
】Android:使用FileProvider的外部存储文件的URI【英文标题】:Android:URIforfileonexternalstorageusingFileProvider【发布时间】:2017-07-0421:08:25【问题描述】:我已将我的应用程序移植到androidAPI24。我的应用程序将文件下载到外部存储中名为t... 查看详情
使用 FileProvider 从图库中选择图像文件
】使用FileProvider从图库中选择图像文件【英文标题】:PickinganimagefilefromGalleryusingFileProvider【发布时间】:2016-12-3001:07:11【问题描述】:为AndroidN编译我遇到了FileProvider的问题。我需要让用户从图库中选择图像/用相机拍照,然后... 查看详情
FileProvider 和辅助外部存储
】FileProvider和辅助外部存储【英文标题】:FileProviderandsecondaryexternalstorage【发布时间】:2017-03-1202:46:39【问题描述】:如何使用FileProvider从SECONDARY外部存储提供文件?FileProvider的当前实现只处理ContextCompat.getExternalFilesDirs返回的... 查看详情
fileprovider解决fileuriexposedexception(代码片段)
...错:android.os.FileUriExposedException:file:///storage/emulated/0/Download/FileProvider.apkexposedbeyondappthroughIntent.getData()从Android7.0开始,一个应用提供自身文件给其它应用使用 查看详情
fileprovider添加二级目录
...式会被认为不安全的然后系统需要让我们使用更加安全的FileProvider的方法去构建intent请求如拍照,安装新的apk包等等。。。这样的适配方法网上很多Manifest里声明provider,xml设置文件目录,代码里获取uri,intent里设置uri,然后使... 查看详情
如何处理无法解码流:java.io.FileNotFoundException:使用 FileProvider(没有这样的文件或目录)
】如何处理无法解码流:java.io.FileNotFoundException:使用FileProvider(没有这样的文件或目录)【英文标题】:HowtohandleUnabletodecodestream:java.io.FileNotFoundException:onusingFileProvider(Nosuchfileordirectory)【发布时间】:2017-07-2905:54:07【问题描述... 查看详情
Android Intent FileProvider“权限被拒绝”在谷歌地球中打开KML文件?
】AndroidIntentFileProvider“权限被拒绝”在谷歌地球中打开KML文件?【英文标题】:AndroidIntentFileProvider"Permisiondenied"toopenKMLfileinGoogleEarth?【发布时间】:2018-07-1910:14:36【问题描述】:在AndroidNougat(7.0)之前,在外部存储中创建K... 查看详情
android打开与分享文件(代码片段)
创建fileProvider<application>//放在application中<providerandroid:name="androidx.core.content.FileProvider"android:authorities="com.h3c.sharedemo.fileprovider"android:export 查看详情
android打开与分享文件(代码片段)
创建fileProvider<application>//放在application中<providerandroid:name="androidx.core.content.FileProvider"android:authorities="com.h3c.sharedemo.fileprovider"android:export 查看详情
android打开与分享文件(代码片段)
创建fileProvider<application>//放在application中<providerandroid:name="androidx.core.content.FileProvider"android:authorities="com.h3c.sharedemo.fileprovider"android:export 查看详情
fileprovider使用及相关第三方冲突的完美解决
...神的文章作参考,这里附上地址:Android7.0行为变更通过FileProvider在应用间共享文件吧为了与时俱进,将项目tagretSdkVersion升到了25,同时进行了对Android7.0的支持。Android7.0之后,应用间传递文件有两种解决方案,一种是开启严苛模... 查看详情
springboot项目使用poi导出excel到指定目录并且从指定目录下载excel文件(代码片段)
...示例截图2、导出excel到指定目录示例代码二、从指定目录下载excel文件1、从指定目录下载excel文件示例截图2、从指定目录下载excel文件示例代码一、导出excel到指定目录1、导出excel到指定目录示例截图导出excel到指定目录,如下图... 查看详情
文件应用程序中的 Apple FileProvider 扩展从文档浏览器上下文菜单操作中删除复制、重复操作
】文件应用程序中的AppleFileProvider扩展从文档浏览器上下文菜单操作中删除复制、重复操作【英文标题】:AppleFileProviderextentioninFileApplicationremoveCopy,Duplicateoperationfromdocumentbrowser\'scontextmenuaction【发布时间】:2018-02-1409:09:44【问题... 查看详情
使用 FileProvider 共享缓存的图像
】使用FileProvider共享缓存的图像【英文标题】:SharingcachedimagesusingFileProvider【发布时间】:2013-11-0812:04:37【问题描述】:我有一个应用程序,它使用UniversalImageLoader从互联网下载照片,将它们缓存到data/data/com.myapp/cache并显示在Ima... 查看详情
python从相对目录打开文件(代码片段)
如何从为目录列表打开的目录中获取文件?
】如何从为目录列表打开的目录中获取文件?【英文标题】:HowtoGetFilesFromADirectoryThat\'sOpenforDirectoryListing?【发布时间】:2012-08-0702:54:04【问题描述】:假设一个网站已经为其文件夹之一设置了目录列表权限,我可以在网络浏览... 查看详情
android打开与分享文件(代码片段)
创建fileProvider<application>//放在application中<providerandroid:name="androidx.core.content.FileProvider"android:authorities="com.h3c.sharedemo.fileprovider"android:exported="false"android:grantUriPermissions="true"><meta-dataandro... 查看详情
wordpress下载后怎么打开安装?
如图,打开下载后的文件夹,啥程序都找不到啊?第一步:解压文件到本地服务器根目录:第二步:访问本地地址,本文是以localhost/wordpress目录进行安装的,点击创建配置文件:第三步:在phpmyadmin里面创建名为wordpress的数据库后... 查看详情