[英]parsing error after launching intent to install apk
所以我有一個下載然后安裝apk的功能。 當意圖啟動時,我得到“解析這個包時出現問題”。 錯誤。 但是,如果我嘗試手動安裝它,它工作正常。
下載Activity.java:
public void onClickUpdate(View view) throws IOException {
Toast.makeText(SettingsActivity.this, "Checking for updates", Toast.LENGTH_SHORT).show();
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).writeTimeout(10,TimeUnit.SECONDS).readTimeout(10,TimeUnit.SECONDS).build();
final Request request=new Request.Builder().url("theurl").addHeader("version",version).get().build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try {
File sdcard=new File("sdcard/Download");
File file=new File(sdcard,"app-debug.apk");
file.createNewFile();
BufferedSink sink=Okio.buffer(Okio.sink(file));
sink.writeAll(Objects.requireNonNull(response.body()).source());
sink.close();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(SettingsActivity.this, "Installed successfully!", Toast.LENGTH_LONG).show();
try{
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uri=Uri.parse("content://"+"sdcard/Download/app-debug.apk");
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);}catch (Exception e){
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}finally {
response.close();
}
}
});
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.testapp.test">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".VersionActivity">
</activity>
<activity android:name=".SettingsActivity">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
此外,我確實嘗試以編程方式安裝 apk 錯誤 - 包解析錯誤,但這根本沒有幫助。
日志:2020-09-29 07:31:13.669 24595-29726/? E/ActivityThread:未能找到 sdcard 的提供者信息 2020-09-29 07:31:13.671 24595-29726/? W/InstallStaging:從內容 URI java.io.FileNotFoundException 暫存 apk 時出錯:無內容提供者:android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1506) 上的 content://sdcard/Download/app-debug.apk .content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1357) at android.content.ContentResolver.openInputStream(ContentResolver.java:1071) at com.android.packageinstaller.InstallStaging$StagingAsyncTask.doInBackground(InstallStaging.java:167) at com. android.packageinstaller.InstallStaging$StagingAsyncTask.doInBackground(InstallStaging.java:161) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java) :636) 在 java.lang.Thread.run(Thread.java:764)
我通過添加以下內容來修復它:
StrictMode.VmPolicy.Builder builder=new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure;
我不建議這樣做。 嘗試用content://
包裝您的文件 URI。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.