我可以下载一个文件并保存在下载文件夹中:
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中手动打开设备的高清.由于图像显示路径.
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 – 意图已启动,但文件未打开.
3 – 调试模式在“new file(urlString)”中给出了这个:“urlString = / storage / emulated / 0 / Download / name.pdf”
4 – 在“fileProvIDer.getUriFromfile …”调试模式具有:
“urifile = content://com.example.androID.parlamentaresapp.fileprovIDer/Download/name.pdf”
谢谢.
解决方法 在与startActivity()和fileProvIDer Uri一起使用的Intent上调用addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION).没有它,活动无权访问您的内容. 总结以上是内存溢出为你收集整理的android – FileProvider – 从下载目录中打开文件全部内容,希望文章能够帮你解决android – FileProvider – 从下载目录中打开文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)