Opening APK File From Within App For Automatic App Update - FileProvider Android - Firebase Storage

Viewed 237

I am attempting to check a FirebaseStorage bucket for a new APK file, if there is a new version, download it, and then open the file to automatically start the installation. Downloading the file is easy, but I cannot seem to get FileProvider to work for opening the file? I'm downloading the file into external storage, specifically into the user's Downloads folder. I'm attempting to open it using an intent, and while no errors are being thrown, nothing is opening and there is definitely no update occurring.

Here's the update function. You can probably ignore most of it until the very middle where I attempt to open the file, the rest is just downloading from Firebase and works just fine.

public void checkForAppUpdate() {
    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    Network activeNetwork = cm.getActiveNetwork();
    boolean isConnected = activeNetwork != null;
    if (isConnected) {


        StorageReference reference = firebaseStorage.getReference().getRoot().child("Apk-Release");
        File rootpathtemp = new File(Environment.getExternalStorageDirectory().getParent(),"");
        final File rootpath = new File(rootpathtemp.getParent(),"/self/primary/Download/");
        if(!rootpath.exists()){
            rootpath.mkdirs();
        }
        System.out.println(settings.this.getExternalFilesDir(null));
        reference.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
            @Override
            public void onSuccess(ListResult listResult) {
                for(final StorageReference item : listResult.getItems()){
                    if(Integer.parseInt(item.getName().replace("_","").replace("ver","").replace(".apk","")) > BuildConfig.VERSION_CODE){
                        final File localfile = new File(rootpath,item.getName());
                        item.getFile(localfile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                                Intent i = new Intent();
                                i.setAction(Intent.ACTION_VIEW);
                                System.out.println(localfile.getAbsolutePath());
                                System.out.println(rootpath.getAbsolutePath());
                                Uri data = getUriForFile(settings.this, settings.this.getApplicationContext().getPackageName() + ".provider",localfile);
                                i.setDataAndType(data,"application/vnd.android.package-archive");
                                i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                settings.this.getApplicationContext().startActivity(i);
                            }
                        });
                        break;
                    }
                }
            }
        });

    }
}

I've added the provider in manifest...

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="com.example.myapp.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

I've included the provider_paths xml document inside of /res/xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="./../../self/primary/Download"
        />
</paths>

This is the internal storage of the device I'm testing on.. so this is kinda what im going off of for storage locations. It seems to be downloading the file in the correct location. enter image description here

0 Answers
Related