0

I build an app and when run app show dialog to the user to download a new version. ok, but I have some problem with it. I want to download an app in my internal storage in folder "Download" and when download finished automatic install it. but I cannot access it. my code : Download Code ->

HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
            String PATH = Environment.getExternalStorageDirectory() + "/Download/";
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new File(file, "pezeshk-yab.apk");
            if (outputFile.exists()) {
                outputFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(outputFile);
            InputStream is = c.getInputStream();
            int total_size = 8404140;//size of apk
            byte[] buffer = new byte[1024];
            int len1;
            int per;
            int downloaded = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
                downloaded += len1;
                per = downloaded * 100 / total_size;
                publishProgress(per);
            }
            fos.close();
            is.close();
            newVersion(PATH);
            flag = true;

And for automatic installed use this code ,I know that these codes have problems, so please correct them :

AndroidManifest.xml ->

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="ir.vian_web.pezeshkyab.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths"/>

Most of my problem with this part is that I do not know what to put here filepaths.xml ->

 <?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="my_app"
        path="" />
</paths>

and method for new version, Most of my problem with this part is that I do not know what to put here ->

 public void newVersion(String path) {
    Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    File file = new File(path, "pezeshk-yab.apk");
    Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", file);
    intent.setData(uri);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);
    finish();
}

And my error when debugging it ->

Failed to find the configured root that contains /storage/emulated/0/Download/pezeshk-yab.apk
Saveen
  • 4,120
  • 14
  • 38
  • 41
Hadi Khezrpor
  • 401
  • 5
  • 21

0 Answers0