2016-09-05 30 views
5

當我的應用程序來處理意圖我有一個自定義自動下載並安裝APK它的工作原理是這樣沒有發現使用FileProvider

// auto register for the complete download 
    activity.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 



// Download the file through DownloadManager 
String destination = Environment.getExternalStorageDirectory() + "/"; 
    String fileName = "myfile.apk"; 
    destination += fileName; 
    final Uri uri = Uri.parse("file://" + destination); 
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl)); 
    request.setDescription("description"); 
    request.setTitle("title"); 
    request.setDestinationUri(uri); 
    final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE); 
    final long downloadId = manager.enqueue(request); 

onComplete = new BroadcastReceiver() { 
     public void onReceive(Context ctxt, Intent intent) { 

      Intent install = new Intent(Intent.ACTION_VIEW); 
      // BEFORE working doing this 
      //install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      //install.setDataAndType(uri, 
      // manager.getMimeTypeForDownloadedFile(downloadId)); 

      // Using file provider it doesnt work 
      Uri apkUri = FileProvider.getUriForFile(AutoUpdate.this, 
       "com.myapp", file); 
       install.setDataAndType(apkUri,manager.getMimeTypeForDownloadedFile(downloadId)); 
      activity.startActivity(install); 
      activity.unregisterReceiver(this); 

     } 
    }; 

Android清單活動:

<provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="com.myapp" 
      android:exported="false" 
      android:grantUriPermissions="true"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/provider_paths"/> 
     </provider> 

Provider_path(對不起,一些之所以這麼切割路徑標記)

外部路徑名= 「MyFolder文件」 路徑= 「」/>

當文件完成下載的onComplete被調用,但activiy不啓動:

無活動處理意向{行動= android.intent.action.VIEW DAT =內容: //com.myapp/myfolder/myfile.apk 典型值=應用程序/ vnd.android.package歸檔FLG = 0x4000000}

當使用普通文件://它的工作

有我是使用文件提供程序時缺失?該活動是否因文件未找到而無法啓動? 我需要額外的許可嗎? (目前我在外部存儲上有INTERNET,READ和WRITE)

+0

'活動是否因文件未找到而啓動?「。消息是找不到活動。 – greenapps

+0

請注意,如果我用「*/*」替換MIME類型,它會打開應用程序選擇,但是當我選擇「文件管理器」時,它會打開並保留在根目錄下。這就是爲什麼我想通過使用FileProvider該文件實際上沒有找到? – Johny19

+0

你在哪裏聲明瞭「文件」變量? – emaillenin

回答

12

軟件包安裝程序僅支持從Android 7.0開始的content方案。在此之前,儘管文檔與—相反,但軟件包安裝程序僅支持file方案。

根據您是否在Android 7.0+上運行(例如通過在Build.VERSION.SDK_INT上分支),您將需要根據您的Intent設置Uri

+1

該死的你谷歌!感謝您指出了這一點! – Johny19

+0

@ Johny19你可以發佈工作解決方案嗎? – emaillenin

0

您的提供者沒有找到,因爲

android:enabled="true" 

在您的清單丟失。

但是,它不會讀取CW的其他答案。

+0

我試過這個。我仍然得到同樣的錯誤 – Johny19