Java tutorial
/* Copyright (c) 2012 Roman Truba Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package touchgallery; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.support.v4.util.LruCache; import android.util.AttributeSet; import android.widget.ImageView.ScaleType; import android.widget.ProgressBar; import android.widget.RelativeLayout; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import com.hedy.usedbook.R; import entity.Constant; import touchgallery.InputStreamWrapper.InputStreamProgressListener; import util.MD5; public class UrlTouchImageView extends RelativeLayout { protected ProgressBar mProgressBar; protected TouchImageView mImageView; protected Context mContext; protected LruCache<String, Bitmap> mBitmapCache; public UrlTouchImageView(Context ctx, LruCache<String, Bitmap> bitmapCache) { super(ctx); mContext = ctx; mBitmapCache = bitmapCache; init(); } public UrlTouchImageView(Context ctx, AttributeSet attrs) { super(ctx, attrs); mContext = ctx; init(); } public TouchImageView getImageView() { return mImageView; } @SuppressWarnings("deprecation") protected void init() { mImageView = new TouchImageView(mContext); LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); mImageView.setLayoutParams(params); this.addView(mImageView); mImageView.setVisibility(GONE); mProgressBar = new ProgressBar(mContext, null, android.R.attr.progressBarStyleHorizontal); params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.CENTER_VERTICAL); params.setMargins(30, 0, 30, 0); mProgressBar.setLayoutParams(params); mProgressBar.setIndeterminate(false); mProgressBar.setMax(100); this.addView(mProgressBar); } public void setUrl(String imageUrl) { new ImageLoadTask().execute(imageUrl); } public class ImageLoadTask extends AsyncTask<String, Integer, Bitmap> { @Override protected Bitmap doInBackground(String... strings) { Bitmap bm = null; String url = strings[0]; bm = mBitmapCache.get(url); if (bm != null) return bm; MD5 md5 = new MD5(); String fileName = md5.getMD5(url) + url.substring(url.lastIndexOf(".")); File file = new File(Constant.TEMPPATH, fileName); if (file.exists()) { bm = BitmapFactory.decodeFile(file.getAbsolutePath()); mBitmapCache.put(url, bm); return bm; } try { URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); int totalLen = conn.getContentLength(); InputStreamWrapper bis = new InputStreamWrapper(is, 8192, totalLen); bis.setProgressListener(new InputStreamProgressListener() { @Override public void onProgress(float progressValue, long bytesLoaded, long bytesTotal) { publishProgress((int) (progressValue * 100)); } }); FileOutputStream fos = new FileOutputStream(file); byte[] buffer = new byte[1024]; int len = 0; while ((len = bis.read(buffer)) != -1) { fos.write(buffer, 0, len); } bm = BitmapFactory.decodeFile(file.getAbsolutePath()); mBitmapCache.put(url, bm); bis.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } return bm; } @Override protected void onPostExecute(Bitmap bitmap) { if (bitmap == null) { mImageView.setScaleType(ScaleType.CENTER); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); mImageView.setImageBitmap(bitmap); } else { mImageView.setScaleType(ScaleType.MATRIX); mImageView.setImageBitmap(bitmap); } mImageView.setVisibility(VISIBLE); mProgressBar.setVisibility(GONE); } @Override protected void onProgressUpdate(Integer... values) { mProgressBar.setProgress(values[0]); } } }