Currently i'm loading images from a url and it is taking way to long and i cannot work out why, sometimes taking longer than 60 seconds to get images that aren't really that big.
My Code:
Get image async task:
public class GetImageAsyncTask extends AsyncTask<Void, Void, Bitmap> {
String url;
OnImageRetrieved listener;
ImageView imageView;
int height;
int width;
public GetImageAsyncTask(String url, ImageView imageView,OnImageRetrieved listener, int height, int width) {
this.url = url;
this.listener = listener;
this.imageView = imageView;
this.height = height;
this.width = width;
}
public interface OnImageRetrieved {
void onImageRetrieved(Bitmap image, ImageView imageview, String url);
}
protected Bitmap doInBackground(Void... params) {
Bitmap image = null;
try {
image = ImageUtilities.decodeSampledBitmapFromUrl(this.url, this.width, this.height);
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
protected void onPostExecute(Bitmap result) {
this.listener.onImageRetrieved(result, this.imageView, this.url);
}
}
public static Bitmap decodeSampledBitmapFromUrl(String url, int reqWidth, int reqHeight) throws IOException {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new java.net.URL(url).openStream(), null, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(new java.net.URL(url).openStream(), null, options);
}
Getting sample size:
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
These methods are used because of memory complications that can arise if not, but the time it seems to take is just to long. Is there some very heavy computation that i'm just not seeing or?
you can use Picasso or volly library to load image.I suggest volly to use because its introduce by google itself.
So the problem stems from the array adapter and the fact that getView() can be called 100's of times which can be close to 100mb of data is being downloaded simultaneously.
So as a temp fix for this situation i implemented a global LruCache singleton, that i first check before starting the async task.
This is clearly not ideal but it will have to do for now. Im sure there are better solutions out there and i would love to hear them if anyone has one to offer.
Related
So i saw this xamarin document about loading large bitmaps efficiently. Yet im struggling to implement it for the gridview.
https://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/
So how can we implement it for a gridview's adapter?
Thank you in the advance.
So how can we implement it for a gridview's adapter?
You can create a class(ThumbImageFactory below) to wrap all the functions mentioned in the document:
public class ThumbImageFactory
{
public readonly Context context;
public ThumbImageFactory(Context c)
{
context = c;
}
public static int CalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
float height = options.OutHeight;
float width = options.OutWidth;
double inSampleSize = 1D;
if (height > reqHeight || width > reqWidth)
{
int halfHeight = (int)(height / 2);
int halfWidth = (int)(width / 2);
// Calculate a inSampleSize that is a power of 2 - the decoder will use a value that is a power of two anyway.
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth)
{
inSampleSize *= 2;
}
}
return (int)inSampleSize;
}
public Bitmap LoadScaledDownBitmapForDisplay(Resources res, BitmapFactory.Options options, int reqWidth, int reqHeight,int resourceId)
{
// Calculate inSampleSize
options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.InJustDecodeBounds = false;
return BitmapFactory.DecodeResource(res, resourceId, options);
}
public BitmapFactory.Options GetBitmapOptionsOfImage(int resourceId)
{
BitmapFactory.Options options = new BitmapFactory.Options
{
InJustDecodeBounds = true
};
// The result will be null because InJustDecodeBounds == true.
Bitmap result = BitmapFactory.DecodeResource(context.Resources, resourceId, options);
int imageHeight = options.OutHeight;
int imageWidth = options.OutWidth;
return options;
}
}
Create an ThumbImageFactory instance in your Adapter and call the functions in GetView:
public class ImageAdapter : BaseAdapter
{
private readonly Context context;
private ThumbImageFactory thumbFactory;
public ImageAdapter(Context c)
{
context = c;
thumbFactory = new ThumbImageFactory(c);
}
...
public override View GetView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null)
{
imageView = new ImageView(this.context);
imageView.LayoutParameters = new AbsListView.LayoutParams(150, 150);
imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
imageView.SetPadding(0, 0, 0, 0);
}
else
{
imageView = (ImageView)convertView;
}
Bitmap bitmap = GetThumbImage(thumbIds[position]);
imageView.SetImageBitmap(bitmap);
return imageView;
}
public Bitmap GetThumbImage(int resourceId)
{
BitmapFactory.Options options = thumbFactory.GetBitmapOptionsOfImage(resourceId);
Bitmap bitmap=thumbFactory.LoadScaledDownBitmapForDisplay(context.Resources, options, 150, 150, resourceId);
return bitmap;
}
}
Notes: we can't modify the GetView to async, so I changed all the functions in document to sync functions. Here is is complete Demo.
I'm trying to solve this issue about mixed up images in gridview when scrolling. I already saw similar posts here about this issue but unfortunately I didn't solve it yet.
I use asynctask to load the images in the gridView, and I have only imageView in the grid.
Thanks!
Here is my code:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView img;
if (convertView == null) {
img = new ImageView(GalleryActivity.this);
WindowManager wm = (WindowManager) GalleryActivity.this.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
width = width / 3 - 4;
img.setLayoutParams(new AbsListView.LayoutParams(width, width));
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
img = (ImageView) convertView;
}
File dics = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File dir = new File(dics, "yyy");
if (!dir.exists() && !dir.mkdirs()) {
img.setImageResource(holder(position + 1));
} else {
String file_name = dir.getAbsolutePath() + "/" + (position + 1) + ".jpg";
if (new File(file_name).exists()) {
BitmapWorkerTask task = new BitmapWorkerTask(img);
task.execute(file_name);
items[position].setStatus(1);
} else {
img.setImageResource(holder(position + 1));
}
}
return img;
}
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private String data;
public BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
#Override
protected Bitmap doInBackground(String... params) {
data = params[0];
return decodeSampledBitmapFromResource(data,100,100);
}
// 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 Bitmap decodeSampledBitmapFromResource(String resId,
int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(resId,options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(resId);
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
The reason behind your problem is that the BitmapWorkerTask is not finished by starting order so it could override the newest result by older one.
To avoid this you have to keep one BitmapWorkerTask for each view and cancel the old one if it's still working so it doesn't override the result of the new BitmapWorkerTask
You can use setTag and getTag methods to achive this purpose and keep a reference of the latest BitmapWorkerTask working on each view.
You are not setting Tag to ImageView while starting image loading thread that why is set image to current one show at screen when thread completes it job .
you should pass some id as a parameter in BitmapWorkerTask class .
I'm trying to make an android app with ListView that has an image in each row. I want to store all the images in SQLite database with BLOB data type and then populate the ListView with these images,but whenever I've got more than 2 jpeg images in the list i get OutOfMemoryError. I already tried many techniques found on the internet to reduce the use of memory but still can't solve this problem. I found the use of options.inJustDecodeBounds and options.inSampleSize in android documentation but still getting the same exception. Below you can find my code with Bitmap to byte array and byte array to bitmap conversion.
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public class ListItem {
private int id;
private byte [] img;
private String title;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public byte[] getImg() {
return img;
}
public void setImg(byte[] img) {
this.img = img;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public static byte[] convertBitmapToBytes(Bitmap bmp){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
public static Bitmap getBitmap(byte [] bitmapdata){
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
ByteArrayInputStream inStream = new ByteArrayInputStream(bitmapdata);
BitmapFactory.decodeStream(inStream, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 50, 50);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeStream(inStream,null,options);
return bmp;
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}
I've got the same issue in a ListView. I solved it by freeing the allocated memory on leaving the activity. For this I called the method "recycle()" for each displayd bitmap.
private List<Bitmap> m_displayedBitmaps = new ArrayList<Bitmap>();
private List<ImageView> m_displayedGridElements = new ArrayList<ImageView>();
// protected void onPause() {
public void onStop() {
// don't forget to free the references to your bitmap
for (ImageView currentGridElement : m_displayedGridElements) {
currentGridElement.setImageBitmap(null);
}
m_displayedGridElements.clear();
// recycle each image
for (Bitmap currentBitmap : m_displayedBitmaps) {
currentBitmap.recycle();
}
// clear the list
m_displayedBitmaps.clear();
super.onStop();
}
Android has a very bad image-memory managment. so you need to be very careful handling images. Fortunatelly you can try with an external library to take care of image display. Try with universal image loader UIL
You should not store images in SQLite. Instead use an image loading/caching library like Glide/Picasso, both of them are extremely simple to use and cache images out of the box.
I followed the online tutorial and created a class like so:
public class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
#Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
return decodeSampledBitmapFromResource(getResources(), data, 100, 100);
}
public 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);
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
// 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) {
BitmapWorkerTask task = new BitmapWorkerTask(imageView);
task.execute(resId);
}
}
Now that I created the "ASyncTask" class, I do not know how to apply it.
What I need to do is simple. I need to draw the bitmap like so (it is set up so that it will do it every second):
canvas.drawBitmap(myBitmap, centerX, centerY, null);
How do I apply ASyncTask now so that I will draw this bitmap without doing too much work on the thread? Please help, much appreciated.
I try a gallery app.
My app working 2 diffrent form.
From the Gallery or from camera selection,
if selected photos height large to width everything ok.
if Selected photos width large to height, photos seems vertical
Example,
This image width : 3264, height : 2448
My app in seems like this;
this is my decode size code, Sorry bad english, thank you.
private class DecodeSize extends AsyncTask<String,Void,Bitmap> {
private int reqWidth;
private int reqHeight;
private ImageView imageView;
private final WeakReference<ImageView> imageViewReference;
private DecodeSize(int reqWidth, int reqHeight, ImageView imageView) {
this.reqWidth = reqWidth;
this.reqHeight = reqHeight;
this.imageViewReference = new WeakReference<ImageView>(imageView);
}
public int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
#Override
protected Bitmap doInBackground(String... params) {
String photo_path = params[0];
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photo_path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(photo_path, options);
}
#Override
protected void onPostExecute(Bitmap bitmap) {
if(imageViewReference != null && bitmap !=null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
private void decodeSizeFunction(String picturePath) {
decodeSize = new DecodeSize(200,200,foto_image);
decodeSize.execute(picturePath);
}