Use org.linphone.utils BitmapAsync class
This commit is contained in:
parent
d1f7a51cf5
commit
bd89b15de7
1 changed files with 4 additions and 127 deletions
|
@ -61,6 +61,8 @@ import org.linphone.utils.FileUtils;
|
||||||
import org.linphone.utils.LinphoneUtils;
|
import org.linphone.utils.LinphoneUtils;
|
||||||
import org.linphone.utils.SelectableAdapter;
|
import org.linphone.utils.SelectableAdapter;
|
||||||
import org.linphone.utils.SelectableHelper;
|
import org.linphone.utils.SelectableHelper;
|
||||||
|
import org.linphone.views.AsyncBitmap;
|
||||||
|
import org.linphone.views.BitmapWorkerTask;
|
||||||
import org.linphone.views.ContactAvatar;
|
import org.linphone.views.ContactAvatar;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -489,7 +491,7 @@ public class ChatMessagesOldAdapter extends SelectableAdapter<ChatMessageOldView
|
||||||
private void loadBitmap(String path, ImageView imageView) {
|
private void loadBitmap(String path, ImageView imageView) {
|
||||||
if (cancelPotentialWork(path, imageView)) {
|
if (cancelPotentialWork(path, imageView)) {
|
||||||
mDefaultBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.chat_file);
|
mDefaultBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.chat_file);
|
||||||
BitmapWorkerTask task = new BitmapWorkerTask(imageView);
|
BitmapWorkerTask task = new BitmapWorkerTask(mContext, imageView, mDefaultBitmap);
|
||||||
final AsyncBitmap asyncBitmap = new AsyncBitmap(mContext.getResources(), mDefaultBitmap, task);
|
final AsyncBitmap asyncBitmap = new AsyncBitmap(mContext.getResources(), mDefaultBitmap, task);
|
||||||
imageView.setImageDrawable(asyncBitmap);
|
imageView.setImageDrawable(asyncBitmap);
|
||||||
task.execute(path);
|
task.execute(path);
|
||||||
|
@ -560,122 +562,8 @@ public class ChatMessagesOldAdapter extends SelectableAdapter<ChatMessageOldView
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Bitmap related classes and methods
|
|
||||||
*/
|
|
||||||
|
|
||||||
private class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
|
|
||||||
private final WeakReference<ImageView> imageViewReference;
|
|
||||||
public String path;
|
|
||||||
|
|
||||||
public BitmapWorkerTask(ImageView imageView) {
|
|
||||||
path = null;
|
|
||||||
// Use a WeakReference to ensure the ImageView can be garbage collected
|
|
||||||
imageViewReference = new WeakReference<>(imageView);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Bitmap scaleToFitHeight(Bitmap b, int height) {
|
|
||||||
float factor = height / (float) b.getHeight();
|
|
||||||
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode image in background.
|
|
||||||
@Override
|
|
||||||
protected Bitmap doInBackground(String... params) {
|
|
||||||
path = params[0];
|
|
||||||
Bitmap bm = null;
|
|
||||||
Bitmap thumbnail = null;
|
|
||||||
if (FileUtils.isExtensionImage(path)) {
|
|
||||||
if (path.startsWith("content")) {
|
|
||||||
try {
|
|
||||||
bm = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), Uri.parse(path));
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
Log.e(e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(e);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
bm = BitmapFactory.decodeFile(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageView imageView = imageViewReference.get();
|
|
||||||
try {
|
|
||||||
// Rotate the bitmap if possible/needed, using EXIF data
|
|
||||||
Matrix matrix = new Matrix();
|
|
||||||
ExifInterface exif = new ExifInterface(path);
|
|
||||||
int pictureOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
|
|
||||||
if (pictureOrientation == 6 || pictureOrientation == 3 || pictureOrientation == 8) {
|
|
||||||
if (pictureOrientation == 6) {
|
|
||||||
matrix.postRotate(90);
|
|
||||||
} else if (pictureOrientation == 3) {
|
|
||||||
matrix.postRotate(180);
|
|
||||||
} else {
|
|
||||||
matrix.postRotate(270);
|
|
||||||
}
|
|
||||||
if (pictureOrientation == 6 || pictureOrientation == 8) {
|
|
||||||
matrix.postScale(1, imageView.getMeasuredHeight() / (float) bm.getHeight());
|
|
||||||
} else {
|
|
||||||
matrix.postScale(imageView.getMeasuredHeight() / (float) bm.getHeight(), 1);
|
|
||||||
}
|
|
||||||
thumbnail = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
|
|
||||||
if (thumbnail != bm) {
|
|
||||||
bm.recycle();
|
|
||||||
bm = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.e(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (thumbnail == null && bm != null) {
|
|
||||||
thumbnail = scaleToFitHeight(bm, imageView.getMeasuredHeight());
|
|
||||||
bm.recycle();
|
|
||||||
}
|
|
||||||
return thumbnail;
|
|
||||||
} else {
|
|
||||||
return mDefaultBitmap;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Once complete, see if ImageView is still around and set bitmap.
|
|
||||||
@Override
|
|
||||||
protected void onPostExecute(Bitmap bitmap) {
|
|
||||||
if (isCancelled()) {
|
|
||||||
bitmap.recycle();
|
|
||||||
bitmap = null;
|
|
||||||
}
|
|
||||||
if (imageViewReference != null && bitmap != null) {
|
|
||||||
final ImageView imageView = imageViewReference.get();
|
|
||||||
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
|
|
||||||
if (this == bitmapWorkerTask && imageView != null) {
|
|
||||||
imageView.setImageBitmap(bitmap);
|
|
||||||
imageView.setTag(path);
|
|
||||||
imageView.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
openFile((String) v.getTag());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class AsyncBitmap extends BitmapDrawable {
|
|
||||||
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
|
|
||||||
|
|
||||||
public AsyncBitmap(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
|
|
||||||
super(res, bitmap);
|
|
||||||
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
|
|
||||||
}
|
|
||||||
|
|
||||||
public BitmapWorkerTask getBitmapWorkerTask() {
|
|
||||||
return bitmapWorkerTaskReference.get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean cancelPotentialWork(String path, ImageView imageView) {
|
private boolean cancelPotentialWork(String path, ImageView imageView) {
|
||||||
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
|
final BitmapWorkerTask bitmapWorkerTask = BitmapWorkerTask.getBitmapWorkerTask(imageView);
|
||||||
|
|
||||||
if (bitmapWorkerTask != null) {
|
if (bitmapWorkerTask != null) {
|
||||||
final String bitmapData = bitmapWorkerTask.path;
|
final String bitmapData = bitmapWorkerTask.path;
|
||||||
|
@ -691,15 +579,4 @@ public class ChatMessagesOldAdapter extends SelectableAdapter<ChatMessageOldView
|
||||||
// No task associated with the ImageView, or an existing task was cancelled
|
// No task associated with the ImageView, or an existing task was cancelled
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
|
|
||||||
if (imageView != null) {
|
|
||||||
final Drawable drawable = imageView.getDrawable();
|
|
||||||
if (drawable instanceof AsyncBitmap) {
|
|
||||||
final AsyncBitmap asyncDrawable = (AsyncBitmap) drawable;
|
|
||||||
return asyncDrawable.getBitmapWorkerTask();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue