Android Open Source - PinterestLikeApp Image Downloader






From Project

Back to project page PinterestLikeApp.

License

The source code is released under:

MIT License

If you think the Android project PinterestLikeApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.dreamtale.pintrestlike.utils;
/*ww w . ja  va2  s .co m*/
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;

import org.apache.http.conn.ConnectTimeoutException;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.dreamtale.pintrestlike.widget.ItemView;

public class ImageDownloader extends AsyncTask<String, Void, Bitmap>
{
    private final static String LOG_TAG = "ImageDownloader";
    
    private ItemView imageView = null;
    
    private String downloadPath;
    
    public ImageDownloader(ItemView imageView)
    {
        this.imageView = imageView;
        downloadPath = (String)imageView.getTag();
    }
    
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }
    
    @Override
    protected Bitmap doInBackground(String... params)
    {
        HttpURLConnection connection = null;
        try
        {
            Log.d(LOG_TAG, "The download url is " + downloadPath);
            URL url = new URL(downloadPath);
            connection = (HttpURLConnection)url.openConnection();
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.connect();
            if (isCancelled())
            {
                Log.d(LOG_TAG, url + " cancled successfully");
                return null;
            }
            int code = connection.getResponseCode();
            if (isCancelled())
            {
                Log.d(LOG_TAG, url + " cancled successfully");
                return null;
            }
            Log.d(LOG_TAG, "The response code is " + code);
            if (code == HttpURLConnection.HTTP_OK)
            {
                BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[4096];
                int count = 0;
                while ((count = is.read(buffer)) != -1)
                {
                    if (isCancelled())
                    {
                        Log.d(LOG_TAG, url + " cancled successfully");
                        return null;
                    }
                    baos.write(buffer, 0, count);
                }
                Bitmap bitmap = ImageUtils.decodeBitmap(baos.toByteArray(), imageView.getMeasuredWidth());
                Log.d(LOG_TAG, "The download bitmap is " + bitmap);
                baos.close();
                if (isCancelled())
                {
                    Log.d(LOG_TAG, url + " cancled successfully");
                    return null;
                }
                return bitmap;
            }
        }
        catch (ConnectTimeoutException e)
        {
            e.printStackTrace();
            Log.d(LOG_TAG, "cause ConnectTimeoutException " + e.toString());
        }
        catch (SocketTimeoutException e)
        {
            e.printStackTrace();
            Log.d(LOG_TAG, "cause SocketTimeoutException " + e.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();
            Log.d(LOG_TAG, "cause Exception " + e.toString());
        }
        finally
        {
            if (null != connection)
            {
                connection.disconnect();
            }
        }
        return null;
    }
    
    @Override
    protected void onPostExecute(Bitmap result)
    {
        if (!imageView.getTag().equals(downloadPath))
        {
            Log.d(LOG_TAG, "The onPostExecute url downloadPath " + downloadPath + " ------------ return -------------");
            return;
        }
        
        if (null != result)
        {
            CacheManager.getInstance().addBitmap2MemoryCache(downloadPath, result);

            imageView.setImageBitmap(result);
        }
        else
        {
            Toast.makeText(imageView.getContext(), "image download failed", Toast.LENGTH_SHORT).show();
        }
    }
}




Java Source Code List

com.dreamtale.pintrestlike.activity.BluetoothDeviceListActivity.java
com.dreamtale.pintrestlike.activity.DetailActivity.java
com.dreamtale.pintrestlike.activity.MainActivity.java
com.dreamtale.pintrestlike.activity.WelcomeActivity.java
com.dreamtale.pintrestlike.data.ImageAdapter.java
com.dreamtale.pintrestlike.data.ImageInfoProvider.java
com.dreamtale.pintrestlike.data.ImageInfo.java
com.dreamtale.pintrestlike.fragment.ImageDetailFragment.java
com.dreamtale.pintrestlike.parser.ImageParser.java
com.dreamtale.pintrestlike.share.BluetoothService.java
com.dreamtale.pintrestlike.utils.CacheManager.java
com.dreamtale.pintrestlike.utils.ImageDownloader.java
com.dreamtale.pintrestlike.utils.ImageUtils.java
com.dreamtale.pintrestlike.utils.IntentConstant.java
com.dreamtale.pintrestlike.utils.UIConfig.java
com.dreamtale.pintrestlike.widget.BluetoothDeviceListDialog.java
com.dreamtale.pintrestlike.widget.ItemView.java
com.dreamtale.pintrestlike.widget.PinterestScrollView.java
com.dreamtale.pintrestlike.widget.PintrestGridView.java