解析错误,在启动意图安装APK后发生。

huangapple go评论77阅读模式
英文:

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 &quot;There was a problem parsing this package.&quot; 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(&quot;sdcard/Download&quot;);
File file=new File(sdcard,&quot;app-debug.apk&quot;);
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, &quot;Installed successfully!&quot;, Toast.LENGTH_LONG).show();
try{
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uri=Uri.parse(&quot;content://&quot;+&quot;sdcard/Download/app-debug.apk&quot;);
intent.setDataAndType(uri, &quot;application/vnd.android.package-archive&quot;);
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"/>

&lt;application
android:allowBackup=&quot;false&quot;
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;@string/app_name&quot;
android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
android:supportsRtl=&quot;true&quot;
android:theme=&quot;@style/AppTheme&quot;&gt;
&lt;activity android:name=&quot;.VersionActivity&quot;&gt;
&lt;/activity&gt;
&lt;activity android:name=&quot;.SettingsActivity&quot;&gt;
&lt;/activity&gt;
&lt;activity android:name=&quot;.MainActivity&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot;/&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot;/&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;/application&gt;

</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.

huangapple
  • 本文由 发表于 2020年9月29日 20:14:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64119406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定