3

I read all of the solutions about this problem.Also I know it can be considered as duplicated but it is not. I see Error: Try Again toast message and I see Update Error log message.

I think at android v26 changed somethings about intent.setDataAndType or I dont know why this is not working.

Also I get the permissions something like this code

ActivityCompat.requestPermissions(Update.this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                REQUEST_WRITE_STORAGE);

Problem can not be solved. I just want to do download and install apk file.

AndroidManifest.xml (I added write permission)

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Update.java

package com.my.testapp;

public class Update extends AppCompatActivity {

ProgressDialog bar;
private static String TAG = "Update";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new DownloadNewVersion().execute();

}

class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        bar = new ProgressDialog(Update.this);
        bar.setCancelable(false);

        bar.setMessage("Downloading...");

        bar.setIndeterminate(true);
        bar.setCanceledOnTouchOutside(false);
        bar.show();

    }

    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);

        bar.setIndeterminate(false);
        bar.setMax(100);
        bar.setProgress(progress[0]);
        String msg = "";
        if(progress[0]>99){

            msg="Finishing... ";

        }else {

            msg="Downloading... "+progress[0]+"%";
        }
        bar.setMessage(msg);

    }
    @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        bar.dismiss();

        if(result){

            Toast.makeText(getApplicationContext(),"Update Done",
                    Toast.LENGTH_SHORT).show();

        }else{

            Toast.makeText(getApplicationContext(),"Error: Try Again",
                    Toast.LENGTH_SHORT).show();

        }

    }

    @Override
    protected Boolean doInBackground(String... arg0) {
        Boolean flag = false;

        try {


            URL url = new URL("http://mydownloadurl.com/_download/update.apk");

            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,"app-debug.apk");

            if(outputFile.exists()){
                outputFile.delete();
            }

            FileOutputStream fos = new FileOutputStream(outputFile);
            InputStream is = c.getInputStream();

            int total_size = 277962;//size of apk

            byte[] buffer = new byte[1024];
            int len1 = 0;
            int per = 0;
            int downloaded=0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
                downloaded +=len1;
                per = (int) (downloaded * 100 / total_size);
                publishProgress(per);
            }
            fos.close();
            is.close();

            OpenNewVersion(PATH);

            flag = true;
        } catch (Exception e) {
            Log.e(TAG, "Update Error: " + e.getMessage());
            flag = false;
        }
        return flag;

    }

}

void OpenNewVersion(String location) {

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(location + "app-debug.apk")),
            "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

}

}

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Berkay Yıldız
  • 440
  • 3
  • 14
  • 1
    https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed – CommonsWare Nov 26 '17 at 23:14
  • Hello thank you, I tried to do this before but I will try it again now I will give info to you in 10 min. – Berkay Yıldız Nov 26 '17 at 23:21
  • Also, modify `Log.e(TAG, "Update Error: " + e.getMessage());` to be `Log.e(TAG, "Update Error: " + e.getMessage(), e);`, so then you can go to LogCat and see the full stack trace associated to any crash that you may be encountering. – CommonsWare Nov 26 '17 at 23:22

1 Answers1

2

Commonsware thanks a lot, I waste my 2-3 hours to do different ways.

Actually I added this code before but the problem was not solved. I tried again and it worked, it might be lazy solution but it is working.

This url have more info about the problem ,other solutions can be applied, but it is enough for me.

        if(Build.VERSION.SDK_INT>=24){
        try{
            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
            m.invoke(null);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

It is totally fixed thank you very much again. Application also need this storage permission.

ActivityCompat.requestPermissions(Update.this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE

Berkay Yıldız
  • 440
  • 3
  • 14