从DownloadManager访问下载的文件

时间:2019-02-13 08:57:52

标签: android android-download-manager android-fileprovider

我正在使用package com.myWebservices.ws; import java.*; import javax.*; @WebService public class GetSomeInfo{ //allow IP to consume web service private static final String IP = "192.168.0.1"; @Resource WebServiceContext wsContext; @WebMethod(operationName="getSomeInfo") public String getSomeInfo(String data) { MessageContext mc = wsContext.getMessageContext(); HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST); //control access by IP inside the web service code if (!(req.getRemoteAddr().equals(IP))) result = "ACCESS DENIED"; else String result = bean.executeSomeProcess(data); return result } } 下载并安装新版本的APK(无法使用Google Play)。

从其他问题中我了解到,从android 7开始 需要使用DownloadManager来启动apk,但出现错误

  

无法找到包含以下内容的配置根目录

下载和接收代码:

FileProvider

和filepaths.xml

    private void startDownload3(String url) {


    final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setTitle(getString(R.string.downloading_update_title));
    String filename = ((int)(Math.random()*100))+getString(R.string.update_file_name_base);
    Logger.e("downloading file = "+filename );
    request.setVisibleInDownloadsUi(true);
    request.setDestinationInExternalPublicDir("/Download", filename);
    saveFileName(filename);

    final long enqueue = dm.enqueue(request);

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                openFile();
            }
        }
    };

    registerReceiver(receiver,
            new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));



}
private void openFile() {
    File[] files = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).listFiles();
    Logger.e("folder Size: "+ files.length);
    String fileName = getFileName(this);
    File realFile=null;
    for (int i = 0; i < files.length; i++)
    { //for testing purposes
        if (fileName.equals(files[i].getName()))
            realFile=files[i];
        Logger.e("file names :  "+ files[i].getName());
    }
    if(realFile!=null) {
        Intent install;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //android 7+
            Uri apkUri = FileProvider.getUriForFile(MyFirebaseMessagingService.this, "mmn.policy.Visitors2.fileprovider", realFile);


            install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            install.setData(apkUri);
            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(install);
        } else {                                            //todo  TEST THIS android 6-
            Uri apkUri = Uri.fromFile(realFile);
            install = new Intent(Intent.ACTION_VIEW);
            install.setDataAndType(apkUri, "application/vnd.android.package-archive");
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(install);
        }}}

和清单

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
     <files-path name="Download" path="Download/" />
 </paths>

现在我可以在测试中看到文件,但 <provider android:name=".GenericFileProvider" android:authorities="com.example.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider> 失败了,是否有办法让FileProvider指向与FileProvider相同的位置?

编辑:完整日志:

setDestinationInExternalPublicDir

感谢进阶!

1 个答案:

答案 0 :(得分:0)

将filepaths.xml中的files-path更改为external-path使其起作用

来自:answer