Android gallery with lazy load - android

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/

Related

Decode bitmap from stream without Apache HTTP Client

Until recently, I decoded images from a web resource using the Apache HTTP Client using this code:
HttpGet httpRequest = new HttpGet(params[0].toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity);
return BitmapFactory.decodeStream(bufferedEntity.getContent());
This all worked perfectly fine.
Now with Android 6, Apache HTTP Client has been deprecated. Not to worry, I thought, just use java.net.HttpUrlConnection instead as recommended here:
http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client
The code I tried and that I found in other questions here is:
HttpURLConnection connection = (HttpURLConnection) params[0].openConnection();
// connection.setRequestProperty("User-Agent", "");
connection.setRequestMethod("GET");
// connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
return bitmap;
This doesn't work. bitmap will always be null for the same image resource that works with the old code.
Does anyone have any insight into this? Here are other questions I tried and why they didn't work:
Android: bitmapfactory.decodestream returns null (Answers use deprecated methods/classes)
Bitmap.decodeStream returns null on specific existing (And working) images (Same as above)
The problem was caused by a simple HTTP/HTTPS issue. The image resource was requested from a http:// address. The server is set up to issue a 307 (temporary redirect) to the matching https:// address.
Although the default of HttpURLConnection is to follow redirects, the code given in the question didn't work. FYI Picasso didn't load the image either.
Requesting the image via its https:// address solved the problem.
Use Volley or Picasso. That's the recommended approach.

Android, How to fit an image into screen?

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);

Posting a video in Facebook through android by using HttpPost

I'm new to Android and I'm trying to develop my own APIs regarding facebook.
I have succeeded to some extent but I have a problem in posting data like videos, comments, photos etc. Can anyone please tell me how to write that using HttpPost and OutputStream.
HttpPost mPost=new HttpPost("https://graph.facebook.com/me/feed?access_token="+URLEncoder.encode(access_token)+"&"+"message="+URLEncoder.encode("hii folks"));
HttpClient mClient=new DefaultHttpClient();
HttpResponse mResp=mClient.execute(mPost);
HttpEntity mEnt=mResp.getEntity();
InputStream is=mEnt.getContent();
Log.w("Response post my status message",convertStreamToString(is));
Don't re-invent the wheel: just use RestFB (at http://restfb.com/). It is quite frankly the next best thing to sliced bread as far as Java/Facebook interaction.
This is publishing a photo:
FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN);
FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,
getClass().getResourceAsStream("/cat.png"), Parameter.with("message", "Test cat"));
You can do exactly the same thing with me/videos to publish a video. It will not handle type conversion of the video file for you but I believe Facebook takes care of that.

Uploading images via http post while preserving EXIF data in Android

I am brand new to Android development, and have run into an issue with my first major app that I would very much appreciate help on. The goal of this app is to allow the user to select an image stored on the phone, and upload that image to a server via a http post method, taking the JPEG binary data as a parameter. The catch is that I need to preserve the EXIF data on this as well. I currently have pushed an image onto my emulator's sdcard, and used a basic app to confirm that it is there with the EXIF data.
Here is what I've got so far:
To provide the user with a choice of images, and as a result obtain the image Uri, I use:
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_IMAGE);
In my activity result code, I obtain my Uri with:
Uri selectedImage = data.getData();
I use this to get a bitmap of the image and display it to the user as a preview.
Here is where I get stuck, as I'm not sue how best to upload it. I have looked a lot at this example in hopes of doing something similar, but he seems to be just compressing a bitmap and sending it, which would probably destroy the EXIF data I need, plus I can't even seem to get that to work.
This is the code I'm trying right now, which given my low experience with such things may be entirely wrong:
Uri selectedImage = data.getData();
InputStream imageInputStream=this.getContentResolver().openInputStream(selectedImage);
String str=imageInputStream.toString();
byte[] imageBits=str.getBytes();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("where I am sending it");
ByteArrayBody toUpload = new ByteArrayBody(imageBits, "androidpic.jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("userImage", toUpload);
reqEntity.addPart("FileName", new StringBody("android pic"));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
This of course does not seem to work.
Does anyone have experience uploading jpegs from the phone like this who could give me some pointers? Any help would be greatly appreciated.

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();

Categories

Resources