package com.ewallet.citygo.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference imageViewReference;
String igstorepath;
boolean isdownloaded=false;
public ImageDownloaderTask(ImageView imageView,String igstorepath) {
imageViewReference = new WeakReference(imageView);
this.igstorepath=igstorepath;
}
private Bitmap downloadBitmap(String url) {
File f = new File(igstorepath);
if(f.exists() && !f.isDirectory()) { //Note that exists() will return true for directories, too
isdownloaded=true;
Log.d("ImageDownloaderTask 已存在:"+igstorepath,""+url);
Bitmap bMap = BitmapFactory.decodeFile(igstorepath);
return bMap;
}else{
isdownloaded=false;
Log.d("ImageDownloaderTask 不存在 ",""+url);
final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
HttpGet getRequest = null;
if(url!=null){
getRequest = new HttpGet(url);
}else{
Log.d("ImageDownloaderTask ","getRequest = new HttpGet(url) null");
}
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode
+ " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// Could provide a more explicit error message for IOException or
// IllegalStateException
getRequest.abort();
Log.w("ImageDownloader", "Error while retrieving bitmap from " + url);
} finally {
if (client != null) {
client.close();
}
}
return null;
}
}
@Override
// Actual download method, run in the task thread
protected Bitmap doInBackground(String... params) {
// params comes from the execute() call: params[0] is the url.
return downloadBitmap(params[0]);
}
@Override
// Once the image is downloaded, associates it to the imageView
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = (ImageView) imageViewReference.get();
if (imageView != null) {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
if(isdownloaded){
//如果下載過的就不存
}else{
File _file = new File(igstorepath);
OutputStream _outStream;
try {
_outStream = new FileOutputStream(_file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, _outStream);
try {
_outStream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
_outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
//imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(R.drawable.basketball));
}
}
}
}
}
沒有留言:
張貼留言