0

When I try this code it get me error when i try to path for install fille--> content then I get "there was a problem parsing the package" error on installation.

This code is in update click event in android studio.


 String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
                                String fileName = "battlegame.apk";
                                destination += fileName;
                                final Uri uri = Uri.parse("file://" + destination);

                                //Delete update file if exists
                                File file = new File(destination);
                                if (file.exists()){

                                    file.delete();
                                 }


                                String url = downloadurl;


                                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                                request.setDescription("Updating....");
                                request.setTitle("Battle Game");


                                request.setDestinationUri(uri);


                                final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                                final long downloadId = manager.enqueue(request);


                                final String finalDestination = destination;
                                BroadcastReceiver onComplete = new BroadcastReceiver() {
                                    public void onReceive(Context ctxt, Intent intent) {


                                        Log.d("Update status", "Download completed   "+uri.toString()+"      id   "+String.valueOf(downloadId));



                                        Intent install = new Intent(Intent.ACTION_VIEW);
                                        install.setDataAndType(Uri.fromFile(new File(finalDestination)),
                                                "application/vnd.android.package-archive");
                                        install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(install);






                                        unregisterReceiver(this);

                                    }
                                };

                                registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

My download was perfectly work but install app not work. below error i get. I pasted logcat for it..


D/Update status: Download completed   file:///storage/emulated/0/Download/battlegame.apk      id   135
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.di.battlegame, PID: 7344
    java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.di.battlegame (has extras) } in com.di.battlegame.FirstActivity$1$2$1@c0b3ffb
        at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1560)
        at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Download/battlegame.apk exposed beyond app through Intent.getData()
        at android.os.StrictMode.onFileUriExposed(StrictMode.java:2083)
        at android.net.Uri.checkFileUriExposed(Uri.java:2388)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10791)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10744)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1703)
        at android.app.Activity.startActivityForResult(Activity.java:5192)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
        at android.app.Activity.startActivityForResult(Activity.java:5150)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
        at android.app.Activity.startActivity(Activity.java:5521)
        at android.app.Activity.startActivity(Activity.java:5489)
        at com.di.battlegame.FirstActivity$1$2$1.onReceive(FirstActivity.java:191)
        at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1550)
        at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
I/Process: Sending signal. PID: 7344 SIG: 9
Process 7344 terminated.

After solving this problem I get this error on android device when I install app.

I get There was a problem parsing the package error.

Click here

for see my problem.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ak23
  • 342
  • 1
  • 5
  • 17
  • Possible duplicate of [android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()](https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed) – Abhinav Suman Nov 22 '19 at 06:51
  • above method I try but not work for me – Ak23 Nov 23 '19 at 04:53
  • my problem is not install app after download i get parse error. – Ak23 Nov 23 '19 at 05:16

1 Answers1

0

Starting from N you can't expose Uri file:// to another apps, use FileProvider instead.

Bracadabra
  • 3,609
  • 3
  • 26
  • 46
  • when i try content for install this error gone but after problem is "There was problem parsing the package" in device. – Ak23 Nov 22 '19 at 05:50
  • Do you have any new exceptions? – Bracadabra Nov 22 '19 at 07:05
  • When I use FileProvider then I get this exeption.... java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.****.***.provider – Ak23 Nov 22 '19 at 08:52
  • I solve above exception but apk not install and logcat not seen anything – Ak23 Nov 22 '19 at 09:32
  • I get "There was a problem parsing the package" error – Ak23 Nov 22 '19 at 09:57
  • If you got java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com then it means you don't declare your provider correctly – Bracadabra Nov 22 '19 at 10:05