简体   繁体   中英

parsing error after launching intent to install apk

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 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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM