0

the code is downloading file from url and save that in external storage work on android 4 - 4.4 but not work on android 5.1 why?

this is my code:

    class DownloadFileFromURL extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread
         * Show Progress Bar Dialog
         * */
        @SuppressWarnings("deprecation")
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(progress_bar_type);
        }

        /**
         * Downloading file in background thread
         * */
        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {


                URL url = new URL(f_url[0]);
                URLConnection conection = url.openConnection();
                conection.connect();
                // getting file length
                int lengthOfFile = conection.getContentLength();

                // input stream to read file - with 8k buffer
                InputStream input = new BufferedInputStream(url.openStream(), 8192);
                // Output stream to write file

                OutputStream output = new FileOutputStream(zippath);
       // /sdcard/downloadedfile.jpg
                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                    publishProgress(""+(int)((total*100)/lengthOfFile));

                    // writing data to file
                    output.write(data, 0, count);
                }

                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }

            return null;
        }

        /**
         * Updating progress bar
         * */
        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
       }

        /**
         * After completing background task
         * Dismiss the progress dialog
         * **/
        @SuppressWarnings("deprecation")
        @Override
        protected void onPostExecute(String src) {
            // dismiss the dialog after the file was downloaded
            dismissDialog(progress_bar_type);


   try {

        Toast.makeText(getApplicationContext(), "download finished",                 Toast.LENGTH_LONG).show();

        Intent ii=new Intent(Download.this,Setpic.class);

        startActivity(ii);
//      

        } catch (Exception e) {
         // TODO: handle exception
         Toast.makeText(getApplicationContext(), ""+e, 5000).show();
 }




        }






    }

and

new DownloadFileFromURL().execute(src);

more details more details more details more details more details

1 Answers1

0

The code you are referencing ("URLConnection") is deprecated, which means it is no longer available in the latest versions of Android. You shouldn't just supress the warning and ignore the message.

The answer has been given here already: HttpEntity is deprecated on Android now, what's the alternative?

M0CH1R0N
  • 756
  • 9
  • 19