Android, How to fit an image into screen? - android

I need to reduce the size of apk file which currently is 40M. This size is because of some high quality images. therefore, i decided to transfer these images to server and load them from there.
I created some html webpages and put each image in its own web page. In app, i used webview to open that link and show the images. However, images are bigger than screen size. previous time because i used image view, i could fit it to screen but now, i'm not sure is it possible to bound web page into screen size.
If you know, please guide me. Thanks

Why don't you use ImageView?
Just create a drawable from stream and use it in your ImageView.
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString); //urlString - url of your image
HttpResponse response = httpClient.execute(request);
InputStream is = response.getEntity().getContent();
Drawable drawable = Drawable.createFromStream(is, "image");
imageView.setImageDrawable(drawable);

Related

Failed uploading larger (size) images from an android app to database

I am calling my REST api made using jersey with POST request and I have put my file as a multipart entity
File file=new File(mImageUri.getPath());
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("image", new FileBody(file));
My code is working when the size of image is relatively small (around less than 1 mb) but is getting failed for larger images.
You have to shrink image to a thumbnail size.
According to android documentation:
For example, it’s not worth loading a 1024x768 pixel image into memory
if it will eventually be displayed in a 128x96 pixel thumbnail in an
ImageView.
Read this SO post.
It seems server side issue. Check on serverside if there is any limit set because by default there is limit inside server side for upload image or video like 2mb or 3 mb.

Why does creating a Drawable from an InputStream cause an OutOfMemoryError when the InputStream comes from a url but not from a asset?

I am working on an app that displays floor plans for buildings. The floor plans are .png files hosted on a server.
This code causes an OutOfMemoryError:
InputStream is = null;
HttpGet httpGet = new HttpGet("http://server/image.png");
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpGet);
is = response.getEntity().getContent();
Drawable d = Drawable.createFromStream(is, null); //this line causes an OutOfMemoryException
imageView.setImageDrawable(d);
But this code does not:
InputStream is = assetManager.open("image.png");
Drawable d = Drawable.createFromStream(is, null);
imageView.setImageDrawable(d);
The images are fairly large in dimension (5100 x 3300 for one example) but that image is only ~100 KB viewing it online, and it's not much smaller (~95 KB) when looking at it in the .apk file.
Does anyone know why the first code snippet causes the error but the second one doesn't? During debugging the first InputStream is only ~102000 bytes in size and the second InputStream is only ~95000 bytes in size. That doesn't seem like a big enough size difference to explain the error.
The drawable stream is going to create a bitmap drawable for it. That means it needs to store the uncompressed full bitmap in memory. To do that, it needs to allocate the entire memory- 5100*3300*4 bytes, or 64 MB. That's way way too large for Android.
When going from assets, it probably isn't reading the file until it has to, and isn't creating an in memory bitmap. It can just keep the file pointer in memory. But you can't do that with a stream, the stream may not be value by the time you get around to reading it, and you can't read it more than once (while you can a file).

Android gallery with lazy load

I have this gallery that loads images from urls, i'm first retrieving an InputStream like this:
HttpParams httpparameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpparameters, 3000);
HttpConnectionParams.setSoTimeout(httpparameters, 5000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpparameters);
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
InputStream is = response.getEntity().getContent();
And then, I read the stream with Bitmap bitmap = BitmapFactory.decodeStream(is, null, opts2);
The problem here is that sometimes i get the out of memory error. I fixed by modifying the inSampleSize, but I dont think its a good workdaround since the quality of the images are not good. Is there any way to load a list of images without getting the outofmemory and with the same quality as in the server? For example, in facebook app, when you enter to the gallery, it do a lazy load, you can see when you are in the grid layout, and when you open the image in full mode, you can scroll and see how it download all the images, and no out of memory issue, or lag while retrieving.
Or if you know of any android library that do this so i dont need to reinvent the wheel
Thank you
There was a nice talk on the Google IO 2012 about IO Gallery application you may easily find on YouTube, with the easy-to-download source code you may get all your answers from: http://code.google.com/p/iogallery/

Does Android keeps the images downloaded from HTTP in cache?

This is how my programme works:
1) Display a picture from the server
2) User change the picture and upload it to the server
3) Display the picture by re-downloading from the server
This is how I get the picture from the server:
String src = "http://www.getyourpicture.com/mypicture.jpg"
HttpGet httpRequest = new HttpGet(URI.create(src) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse)httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
Bitmap dp = BitmapFactory.decodeStream(instream);
//display dp from here...
The problem here is, whenever I "re-download" the image, it still shows the old picture.
To confirm that I've uploaded the picture, I've checked the folder containing the picture on the server and even visited the link on a browser. Both approaches show that the picture is indeed been uploaded. So I've narrowed down to the possibility that Android might have a http caching manager that is not "refreshing" the image link.
So, if the answer to my question is "yes", how can I force the application to not use the cache?
If the answer is "no", what is the thing that I've overlooked?
I'm not sure about the undercovers working and the defaults of HTTP request caching on Android, but if it is decent, then it should in theory suffice to add a query string with a timestamp to the request URL to trigger a brand new and fullworthy HTTP request.
String src = "http://www.getyourpicture.com/mypicture.jpg?" + System.currentTimeMillis();

Android: Getting Drawable from URL not working for .jpg, only works for .png

I am working on and Android app that pulls a picture from an internet page (specifically xckd.com). I have it working wonderfully using code of this basic form (see below)
ImageView iv = new ImageView;
URL url = new URL(address);
InputStream content = (InputStream)url.getContent();
Drawable d = Drawable.createFromStream(content , "src");
iv.setImageDrawable(d)
I noticed when I was viewing some of their older comics the image would not show (but I was scraping the other information from the page properly so I know the url is correct). I determined that this result only occurs when the image is a .jpg file but works perfectly when it is a .png
I have Googled around plenty and I can't figure out why this is, is there a simple fix for this?
It's a known issue http://code.google.com/p/android/issues/detail?id=6066. Using FlushedInputStream solves it.

Categories

Resources