Skip to content

Instantly share code, notes, and snippets.

@awave1
Created August 20, 2016 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awave1/9feb2696555835e9eb1e7026c9a97735 to your computer and use it in GitHub Desktop.
Save awave1/9feb2696555835e9eb1e7026c9a97735 to your computer and use it in GitHub Desktop.
/**
* Created by awave on 2016-07-27.
*/
public class BitmapWorkers {
public static class BitmapWorkerFile extends AsyncTask<String, Void, Bitmap> {
private WeakReference<ImageView> imageViewReference = null;
private Activity mActivity;
public BitmapWorkerFile() {
}
private BitmapWorkerFile(ImageView imageView, Activity a) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<>(imageView);
mActivity = a;
}
@Override
protected Bitmap doInBackground(String... strings) {
return decodeSampledBitmapFromPath(strings[0], 300, 300);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
private static Bitmap decodeSampledBitmapFromPath(String path,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public void loadBitmap(String path, ImageView imageView, Activity a) {
BitmapWorkerFile task = new BitmapWorkerFile(imageView, a);
task.execute(path);
}
}
public static class BitmapWorkerRes extends AsyncTask<Integer, Void, Bitmap> {
private WeakReference<ImageView> imageViewReference = null;
private Activity mActivity;
private int data = 0;
public BitmapWorkerRes() {
}
private BitmapWorkerRes(ImageView imageView, Activity a) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<>(imageView);
mActivity = a;
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
return decodeSampledBitmapFromResource(mActivity.getResources(), data, 800, 400);
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
public void loadBitmap(int resId, ImageView imageView, Activity a) {
BitmapWorkerRes task = new BitmapWorkerRes(imageView, a);
task.execute(resId);
}
private static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment