Before to say this is a duplicate, please read as I did not find my answer on the other topics. I am trying to resize (if needed) a bitmap to a specific size but this size is in Ko (not height/width).
The problem is the following
1) In my app, the user can select pictures from facebook, gallery or the camera
2) I get all of those as bitmap format and they all came with different width to height ratio and different compression rate.
3) I need to store those pictures on my server so that the user can see them in my app at a later point if he comes back. But I want to save a version of about 200/100Ko for each picture.
4) The problem is that if I resize the bitmap to be the same size say 800*600, and then compress the picture to 50% then some bitmaps (that were compressed) will basically take more space than if I was doing nothing and they will not look as good too as 2 layer of compression will be done.
5) I need to find a way to compress my bitmap only if it needs to be compressed and not increasing his size by doing those manipulations. The documentation on this topic looks much more limited, as every questions asked about resizing a picture to a certain size in width/height but not in terms of space.
If any one has links or recommendations on this, would be very happy to hear from you, thanks
Related
I'm taking whole screenshot of a WebView and display the bitmap on an ImageView. The bitmap can be 7 screen height. (E.g. 1440x14000 px)
I'm frequently face with
OutOfMemoryError.
I've seen this
This says that load a scaled down version into memory but I don't want to lose image quality. There are the same approaches on the web.
Is there any way to handle OutOfMemoryError without loading scaled down version?
The bitmap can be 7 screen height. (E.g. 1440x14000 px)
Note that this means that the user cannot see the whole image at once at full resolution.
I'm frequently face with OutOfMemoryError
On most devices, you will have a very difficult time loading an image that large, as you cannot get a single contiguous memory block that big.
This says that load a scaled down version into memory but I don't want to lose image quality
To some extent, you do not have much of a choice. If you want the user to see the full extent of the picture at once, the image has to be scaled to fit the screen.
Is there any way to handle OutOfMemoryError without loading scaled down version?
There are ImageView replacements that offer pan and zoom. Some of those, such as this one, handle loading in pieces of the image at a time, with whatever scaling is necessary for the current zoom level, to make it more likely that you will be able to show the user the entire image.
It is not a solution, of course, but I'm also not familiar with your exact needs, so maybe this may help you a little - you can try to play with bitmap options during decoding. Try to use Bitmap.Config.inPreferredConfig as RGB_565 - this will reduce size of your bitmap twice comparing to default ARGB_8888. But, of course, if you use complex images in your web page this may reduce their quality.
I want to have the posting-a-photo usecase in my android app.
This is the scenario I have implemented:
1. User posts a photo.
2. On the client side I scale the photo* , compress it and send
it to the server.
3. I store the photo on the server.
4. Users, who can view the photo, fetch the photo from the server.
The problem with this approach is that it doesn't take into consideration
the different-screen-sizes problem.
Let's say user A posts a photo.
User B, which has a phone with a larger screen**, fetches the photo
from the server. The photo doesn't fit good in the environment
since it has fixed width and height determined by user A (scaling in step 2).
I cannot simply scale the photo and put it into user B's ImageView, because
I am risking to loose the aspect ratio of the photo.
One approach that comes to mind is to simply remove the step 2 from the scenario.
I send the photo to the server, and when a user tries to fetch it
I scale the image according to the user's screen size (which I receive as parameters in the fetch request).
The problem with this approach is that, on each request, I will have
to scale the photo on-the-fly and send it to the client.
Another problem is the efficiency of uploading a photo, because I don't scale the photo (before uploading it).
It takes much more time to upload a full 2MB (or even a 500KB) photo than to simply upload a scaled photo (~20KB).
What are my options?
Since I am relatively new to Android, chances are that I am missing something essential.
Cheers!
*I scale maintaining the aspect ratio.
** Sorry for the terminology, i don't know how and when to use high density/pixels correctly. :-)
I have own dedicated server with unlimited traffic and 4T HDD. So, basically what I do is:
User take a picture by phone
User upload image to the webserver
Using ImageMagick + mozjpeg 3 I resize image 3 times and save it with prefixes small_, medium_, large_ OR with prefixes pre size: 1024_, 640_...
Now, my app can find user's screen dimensions and load image by doing request for specified prefix.
My arguments for doing in this war are:
Free traffic
Big HDD
Fast user's internet speed
When upload in another thread, and show to user another activity, he doesn't feel the "long waiting time" for uploading large images.
You always can resize photo on device, from 10Mb to 2Mb, for example, and on server do other resizes.
Now about image ratio. There you cannot to do a lot. You must decide, what to do in case that user with large screen and own aspect ratio tries to load image that was uploaded by user with small screen and own aspect ratio.
The truth is that screen size doesn't matter. What is matter is a ratio. So, you can load image, put it in ImageView, and this will filled with black background if the image is "smaller". O r you can load large size of image, crop it and put it to the ImageView. Larger image is used only for better quality.
In my case, I always do crop images if I use it as preview. I think it's looks more beautiful. When user want to view full size image, I download full image and show it as is.
A good server that handles images should:
receive images at high-quality
save or cache the images in multiple sizes (small, medium, large)
serve images according to a size parameter (larger phones ask for the large image)
There are many solutions for this kind of server. Most of them resize the image on-the-fly and cache the result. The client tells the server what size it wants. One example is the image handling in Google Play. Go to the play webside and view the urls of the app-icons (example). They have a width parameter.
You can also think of a maximum dimension (like 2048px) and resize the image before upload only if needed.
The client that views the image needs to decide on what size it asks from the server or it can resize the image on the phone, instead of relying on the server. Do this if traffic is not an issue. Consider using Picasso or Glide to make things easier.
Regarding aspect ratio: The client that views the image should handle this. It should expect any aspect ratio and be prepared for it. Use android:scaleType on the ImageView and decide which scale type fits your scenario. Here are the scale types. If you don't want black lines and want to occupy as much as possible from the screen, use CENTER_CROP - but you might end up losing parts of the image.
In most apps the original aspect ratio in maintained and two black "stripes" are added if your screen doesn't match.
I think it's not possible to manage all screen ratios...
ImageView has android:scaleType attr that preserves aspect ration to be broken,
like fitCenter or centerInside
And also calculate size of fetched image and set view size by yourself.
I am working on a very simple app that shows one jpg, which is scrollable on vertical axis.
I would like this image to be very large(20000x1000 px), however, when I try to run the app on my device, it says that "bitmap too large to be uploaded into a texture".
Is there a way to display the image of such size in an android app?
If not, would it be possible to divide the image into segments, and after I scrolled to the bottom
of one segment, I would proceed to another?
To quote the answer for your question here:
All rendering is based on OpenGL, so no you can't go over this limit.
Note that this would take a huge amount of memory. With such big
images, if you want to zoom in out, and in a mobile environement with
heavy memory constraints, you should setup a system similar to what
you see in google maps for example. With the image split in several
pieces, and several definitions.
You could split the images into let's say 128x128 chunks. Add them to an array, and loop the array to create and fill an ImageView with the image that is currently served.
some pseudocode would be: (excuse me, I've been programming a PHP application the past few days)
Private BitMap[] imageArray = {your bitmaps from internal or external storage};
For(BitMap bm in imageArray) {
// create a new image view here, use the correct layout params or use a parrent grid view.
imageView.setBitMap(bm)
}
now once again, I am 0% sure about that pseudo code, but it should help you along.
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 am writing a Android app which need to display some high quality picture(took from professional DSLR). The problem is it can't be display from gallery.
I choose a photo in Gallery first. The target picture is 2464*1632 JPEG, roughly 4.5M;
Then I just need to compress it to 800*600 and display it in imageview:
image.setImageBitmap(this.bmp);
Thing is that I have tested other image I downloaded form internet(really low quality), and it works without any problem. Can anybody tell me why it can't be displayed? I will be really appericiated
Large images are tricky to handle due to limited memory. You have several choices:
Use a WebView (this allows you to have pinch and zoom functionality to make use of those extra pixels
Decode the image down to the size of the display and then put it in an ImageView using BitmapOpts http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html and changing inSampleSize. It seems you may be having difficulty with that, so consider using createScaledBitmap which just needs the dest width and height.