package com.ved.musicmapapp.utils;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {
RelativeLayout layout = null;
ImageView imageView = null;
String artistName = "";
Context context;
public DownloadImagesTask(String artistName, Context context) {
super();
this.artistName = artistName;
this.context = context;
}
@Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
String urlPath = (String)imageView.getTag();
//Log.v("urlpath...", "..." + urlPath);
Bitmap artistbitmap = download_Image(urlPath);
if(artistbitmap != null){
UtilTask.addBitmapToMemoryCache(urlPath, artistbitmap);
}
return artistbitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
try{
android.view.Display display = ((android.view.WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int size = (int) (display.getWidth() * 0.95f);
result = Bitmap.createScaledBitmap(result, size, size, true);
imageView.setImageBitmap(result);
}catch(Exception e){
//Log.e("DownloadException", "..." + e.getMessage() + "..." + imageView.getTag());
}
}
private Bitmap download_Image(String url) {
BitmapFactory.Options opts=new BitmapFactory.Options();
opts.inDither=false;
opts.inSampleSize = 8;
opts.inPurgeable=true;
opts.inInputShareable=true;
opts.inTempStorage=new byte[16 * 1024];
Bitmap bmp =null;
try{
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
}catch(Exception e){}
return bmp;
}
}