I have a listview that has a photograph from the gallery for each entry but it was blowing up and running out of memory so I resized the image
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 8;
Bitmap yourSelectedImage = BitmapFactory.decodeFile(photo,opts);
imgView.setImageBitmap(yourSelectedImage);
The problem I have is that each image in the list has to be resized before it is displaying the view.
Is there a way to link to the thumbnail view instead of the full size image so that I don't have to resize them or is there a way to load each picture as it is sized instead of having to wait for them all.
This basically sounds like exactly the sort of thing you want from droid-fu's remote image handling, only simpler (the "remote loading" part is really just scaling an image loaded from the Gallery instead of downloading one from the Web). You should be able to use very similar code. Alternatively, this is pretty close to this SO question about lazy-loading images.
Related
I've a list of installed applications that I display in a RecyclerView.
The application icon is stored in a byte array and I use Glide to bind it to an ImageView using:
Glide.with(itemView.getContext())
.load(application.getIcon())
.into(icon);
The problem is that it tooks about 500-700 ms for the icon to be displayed, then it appears in a nice and smooth way. But I don't want that delay nor want to fallback to a placeholder.
If I change Glide code for direct loading (in main application thread) then the icons appear instantly:
byte[] image = application.getIcon();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeByteArray(image,0,image.length,options);
icon.setImageBitmap(bitmap);
But the problem is that If I scroll fast the list sometimes there are some little glitches.
Is there something that I can do to load the icons faster with Glide?
I've tried with .dontAnimate() and .dontTransform() but I haven't seen any differences. What can I do to load the images instantly and get similar results than loading directly in main thread?
My application uses a SurfaceView to show an image to the user, and have them manipulate stickers on top of the image that has been taken. To reduce memory usage, I have scaled all of these bitmaps to fit within the screen. Now I want to save the image that the user just put all of the stickers on, and I would like to save it with the resolution of the original image. How would a go about doing this without loading the full-size images into memory and risking an OutOfMemoryError? I do not know where to start with this, it seems like an impossible task with the given tools.
In case if you want to save the bitmap without downsizing it. There are two options.
Use largeHeap=true in your Application tag (Manifest).
If you can afford a bit of loss in quality! Compress the bitmap using.
Sample Code
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
Below is the link to the documentation of compress method of Bitmap class.
http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)
I would do the following: You have to decouple the editing and the composition. In editing you save what the user wants in some POJOs ( e.g. sticker number A at position X,Y with rotation R )
Then in the composition-step you work with the Hi-Res images - but only one by one to save memory.
In the worst case you have to work with tiles if you still hit memory constraints.
I have a gallery working in my app at the moment with about 8 pictures on it. Before i had a bug that use to make it crash because of overload Heap (something like that, not sure).
This was caused by my pictures being too big in file size, so i reduced them it worked. So my main question is How many picture can i actually put in to my app.
I was hoping to have about 5 screens (activities) with some scrolling pictures
I don't want to start changing my app if its just going to crash again
Also does anyone know how to add transitions when scrolling through pictures, for it too look fancy
Everything depends on the way you manage you pictures. To save the memory (and increase the number of images loaded) you should load a resized picture.
You can subsample picture while loading:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2;
BitmapFactory.decodeResource(res, id, opts);
This will load a picture scaled 1/2 of it's original size (keep in mind that subsampling works only for power of 2 values). You can load the picture smaller than the area it will be displayed in and let ImageView to upscale it.
If your loaded image is still too large, you can scale it more with Bitmap.createScaledBitmap().
You should be aware that pre-Honeycomb Android doesn't recycle bitmaps like post-Honeycomb Android does. You should call Bitmap.recycle() as soon as the bitmap is not used anymore to clear the memory.
For image scrolling you can use ViewPager.
Further reading: http://developer.android.com/training/displaying-bitmaps/index.html
There is no strict limit on the number of pictures you can include in your app. See the stock Gallery app as an example, I consider myself a relatively light picture taker, but I still have hundreds of images in my Gallery. If you follow the Bitmap best practices you shouldn't run into any memory issues, and you should be able to include as many images as you want.
I have a bunch of "large" 400x400 images that I am pulling from a source on the web and I need to display as 100x100 thumbnails in a ListView on Android. If I simply set the dimensions on the imageview element in xml it is very clear it is only scrunching the images down to 100x100 for 2 reasons:
1) The images don't look resampled
2) When given more than lets say 10 of these images in the list the scrolling becomes unbearably slow
I am not AS worried about #1 but the scrolling needs to stay smooth. So I am wondering, how should I go about resizing/resampling these images and caching them? I've done some googling but I just can't find a good resource for what would be a best practice in this situation.
Thanks in advance!
Take a look at BitmapFactory and the Options class. Options has an inSampleSize value that you can set to scale down an image when you read it in. You could try something like
Options op = new Options();
op.inSampleSize = 4;//You want to scale from 400 to 100
Bitmap thumbnail = BitmapFactory.decodeFile(fileName, op);
Just set android:scaleType="fitCenter" and your image will be scaled down to your desired size.
i am working on image ,upload original image in this original image resize the small image this image saved in to database
original image --> resize it (small image )--> small image saved in to database
please forward solution
thanks
To do this you'll want to play around with the Bitmap class.
It has several functions to create new bitmaps form previous ones
Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, width, height, true);
a full list can be found in the documentation :
http://developer.android.com/reference/android/graphics/Bitmap.html
Regarding the save to a database I've never done but it would depend on your server.