简体   繁体   中英

Android horizontal progress bar not working while downloading an image

I'm trying to download an image while using a horizontal progress bar then storing it in a file in my SD card, the code is the following :

  package com.erc.library;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Splashscreen extends Activity {

    ProgressBar pd;
    String a="";
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splashscreen);

        pd=(ProgressBar)findViewById(R.id.progressBar1);
        pd.setMax(100);




        new ProgressTask().execute();
    }


    private class ProgressTask extends AsyncTask <String,String,String>
    {

        @Override
        protected void onPreExecute()
        {

             pd.setIndeterminate(false);
             pd.setMax(100);
             Toast.makeText(getBaseContext(),a,Toast.LENGTH_LONG).show();
        }


        @Override
        protected void onProgressUpdate(String... progress) {

             pd.setProgress(Integer.parseInt(progress[0]));
        }


        protected void onPostExecute(Void result) 
        {
            Toast.makeText(getBaseContext(),"done",Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            ///////////////////////////////////////////////////////////////////////////////////////////////////////
             try
                {
                    URL url = new URL("http://mylink/myimage.png");

                    URLConnection ucon = url.openConnection();
//                  ucon.setReadTimeout(15000);
//                  ucon.setConnectTimeout(30000);

                    InputStream is = ucon.getInputStream();
                    int lenghtOfFile = ucon.getContentLength();
                    BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);

                    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath()+"/library";
                    String fileName = "test.png";

                    // Not sure if the / is on the path or not
                    File file = new File(baseDir + File.separator + fileName);



                //    File file = new File(this.getDir(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Sayegh library", Context.MODE_PRIVATE) + "/yourfile.png");

                    if (file.exists())
                    {
                        file.delete();
                    }
                    file.createNewFile();

                    FileOutputStream outStream = new FileOutputStream(file);
                    byte[] buff = new byte[5 * 1024];

                    int len;
                    byte data[] = new byte[1024];
                       long total = 0;
                    while ((len = inStream.read(buff)) != -1)
                    {
                        total += len;
                          // publishing the progress....
                          // After this onProgressUpdate will be called
                     //  pd.setProgress((int) ((total * 100) / lenghtOfFile));
                      //    publishProgress("" + (int) ((total * 100) / lenghtOfFile));
                        outStream.write(buff, 0, len);
                    }

                    outStream.flush();
                    outStream.close();
                    inStream.close();

                }
                catch (Exception e)
                {
                a=e.toString(); 

                }

            ///////////////////////////////////////////////////////////////////////////////////////////////////////



            return null;

        }


    }

}

Well the progress bar is not showing any progress and the image isn't being downloaded, any help please ?

Looks like you need to set permissions. If it does not help, please read the adb logcat output and post the relevant part here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM