英文:
parsing error after launching intent to install apk
问题
以下是您提供的代码的翻译部分:
public void onClickUpdate(View view) throws IOException {
Toast.makeText(SettingsActivity.this, "正在检查更新", 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, "安装成功!", 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>
日志:
2020-09-29 07:31:13.669 24595-29726/? E/ActivityThread: 无法找到 sdcard 的内容提供程序信息
2020-09-29 07:31:13.671 24595-29726/? W/InstallStaging: 从内容 URI 进行分阶段 apk 时出错
java.io.FileNotFoundException: 无内容提供程序: content://sdcard/Download/app-debug.apk
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1506)
at android.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)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
<details>
<summary>英文:</summary>
So i have a function which downloads and then installs the apk. When the intent launches I get "There was a problem parsing this package." error. However,if i try installing it manually it works fine.
DownloadActivity.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>
Also I did try https://stackoverflow.com/questions/37253879/android-install-apk-programmatically-error-package-parse-error but that did not help at all.
the logs:
2020-09-29 07:31:13.669 24595-29726/? E/ActivityThread: Failed to find provider info for sdcard
2020-09-29 07:31:13.671 24595-29726/? W/InstallStaging: Error staging apk from content URI
java.io.FileNotFoundException: No content provider: content://sdcard/Download/app-debug.apk
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1506)
at android.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)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
</details>
# 答案1
**得分**: 0
我通过添加以下内容进行了修复:
```java
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
我不建议这样做。
尝试将文件URI包装在content://
中,而不是这样做。
英文:
I fixed it by adding this:
StrictMode.VmPolicy.Builder builder=new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure;
I don't recommend doing this.
Try wrapping your file URI with content://
instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论