图像无法从Async Task下载

时间:2016-06-15 08:11:28

标签: android android-asynctask

我正在尝试通过异步任务按钮点击下载图像,但图像未下载。字符串" image_url"?

中的下载链接是否有任何问题

Logcat - skia:--- SkImageDecoder :: Factory返回null

public class DownloadImageAsyncTask extends AppCompatActivity {

Button button;
ImageView imageView;
String image_url = "http://www.freegreatdesign.com/files/images/6/2921-large-apple-icon-png-1.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.download_image_async_task);
    button = (Button) findViewById(R.id.bDownload);
    imageView = (ImageView) findViewById(R.id.downImage);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DownloadTask downloadTask = new DownloadTask();
            downloadTask.execute(image_url);
        }
    });
}

class DownloadTask extends AsyncTask<String, Integer, String>
{
    ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(DownloadImageAsyncTask.this);
        progressDialog.setTitle("Download in progress...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        String path = params[0];
        int file_length = 0;

        try {
            URL url = new URL(path);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            file_length = urlConnection.getContentLength();
            File new_folder = new File("sdcard/photoalbum");

            if(!new_folder.exists())
            {
                new_folder.mkdir();
            }
            File input_file = new File(new_folder, "downloaded_image.jpg");
            InputStream inputStream = new BufferedInputStream(url.openStream(), 8192);
            byte[] data = new byte[1024];
            int total = 0;
            int count = 0;
            OutputStream outputStream = new FileOutputStream(input_file);

            while((count=inputStream.read())!=-1)
            {
                total+=count;
                outputStream.write(data, 0, count);
                int progress = (int) total*100/file_length;
                publishProgress(progress);
            }
            inputStream.close();
            outputStream.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Download Complete...";
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        progressDialog.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        progressDialog.hide();
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
        String path = "sdcard/photoalbum/downloaded_image.jpg";
        imageView.setImageDrawable(Drawable.createFromPath(path));
    }
}
}

0 个答案:

没有答案
相关问题