android下载文件工作但在Android 5.1中无法正常工作

时间:2016-02-02 05:56:40

标签: android android-5.1.1-lollipop

代码从url下载文件并将其保存在外部存储中 在android 4 - 4.4上工作,但不能在Android 5.1上工作 为什么呢?

这是我的代码:

    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();
 }




        }






    }

new DownloadFileFromURL().execute(src);

更多细节 更多细节 更多细节 更多细节 更多细节

1 个答案:

答案 0 :(得分:0)

不推荐使用您引用的代码(“URLConnection”),这意味着它在最新版本的Android中不再可用。你不应该只是压制警告而忽略这条消息。

这里已经给出了答案:HttpEntity is deprecated on Android now, what's the alternative?