How can i use one HttpURLConnection download many pictures from one website? - android

My app should download many pictures from one website,the number is more than 200.So if i put this below code in a for(int i = 0; i++ ; i < 200),it looks not nice,the connection should connect and disconnect every time.
So anybody have some good suggestions?
URL imageUrl = new URL(url);
conn = (HttpURLConnection)(imageUrl.openConnection());
conn.connect();
InputStream is = conn.getInputStream();
BitmapFactory.Options ops = new BitmapFactory.Options();
ops.inSampleSize = inSample;
bitmap = BitmapFactory.decodeStream(is, null, ops);
is.close();
conn.disconnect();

URLConnection pooling happens behind the scenes. You don't have to worry about it yourself.

Since each image have a separate url, connection have to open and closed. There are no alternatives for this code.

Related

Should BufferedInputStream be used when fetching bitmap data using HttpURLConnection?

I've read that wrapping a BufferedInputStream around an input stream is only of benefit if you're reading the input stream in small chunks. Otherwise, using it may actually have adverse effects.
What is the situation when the input stream is bitmap data fetched with HttpURLConnection (or your favourite networking library, e.g. OkHttp)... is it a help or a hindrance?
I'm wondering not only in terms of overall time/speed, but also in terms of resiliency... would using a buffer help at all with flaky network conditions where the connection is dropping in and out?
boolean useBufferedInputStream = true; // <--- true or false?
URL url = new URL("https://example.com/my_bitmap.png");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream;
if (useBufferedInputStream) {
inputStream = new BufferedInputStream(connection.getInputStream());
} else {
inputStream = connection.getInputStream();
}
Bitmap bmp = BitmapFactory.decodeStream(inputStream);

Download gif from given URL

I'm trying to download GIF from given Url but there is a problem. I'm stuck here and don't know the correct way to download this file. How to repair this? I have sample url: http://i1.kwejk.pl/k/obrazki/2015/02/6f0239004a643dbd9510ebda5a6f22b3.gif
My code:
try{
System.out.println(this.adressToGif);
URL url = new URL(urltoGif);
URLConnection conn = url.openConnection();
//conn.connect();
InputStream is = conn.getInputStream(); //IT crashes here and jumps to catch()
BufferedInputStream bis = new BufferedInputStream(is);
bis.mark(conn.getContentLength());
bis.close();
gifPlayer = new NewGifPlayer(this, bis);
//Movie movie = Movie.decodeStream(bis);
}catch(Exception x)
{
System.out.println("There is a problem");
}
You cannot run networking operations on the main thread. Use AsyncTask for that.

Android, how to load 'gz' file from URL and automatically decompress content on load? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android: HTTP communication should use “Accept-Encoding: gzip”
My Android application downloads temp.bin file but today it (file) too big. I configured Apache to compress it to temp.bin.gz by using gzip. So i got 10% from actual size.
So far so good. Now the problem is how to decompress it in Android side during downloading.
I used before this snippets of download algorithm:
URL url = new URL(urlStr);
// set redirect mode ...
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(true);
connection.setConnectTimeout(0);
connection.setReadTimeout(0);
connection.setUseCaches(false);
connection.setAllowUserInteraction(false);
connection.connect();
....
InputStream in = null;
//boolean lengthValid = (contentLength != -1);
byte bytes[] = new byte[256];
int total_count = 0;
int max_count = 0;
try {
in = connection.getInputStream();
int count = 0;
while (count != -1){
count = in.read(bytes);
....
Any and all suggestions would be greatly appreciated.
Simply use java.util.zip.GZIPInputStream instead of InputStream
Also See

android :images not being displayed

I am making an android app in which i need to display the remote images in my app
my using following code.
but the images are not being displayed:
for(int i=0;i<stringOnTextView.length;i++){
imageUrl = "http://ondamove.it/English/images/users/";
imageUrl = imageUrl+stringOnTextView[i];
System.out.println(imageUrl);
URL myFileUrl = new URL(imageUrl);
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
image.setImageBitmap(bmImg);
System.out.println(bmImg.toString());
}
Can anyone tell me where is the problem. thanks
as a completion of my comment , see this three tutorials and you will find out what you should do :) :
tuto1
tuto2
tuto3

How to work with an image using url in android?

Given a Url for an image, I want to downoload it and paste it onto my canvas in android. How do I retrieve the image into my app ?
Please help.
Thanks,
de costo.
Dont forget to give the app the permission to connect to the Web,
in the AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
You can use the following code to download an image:
URLConnection connection = uri.toURL().openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 8 * 1024);
Bitmap bmp = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
Requires the following permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
There is a an HTTP client library that might be supported in Android now, but for any fine grain control you can use URL & HttpURLConnection. the code will look something like this:
URL connectURL = new URL(<your URL goes here>);
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
// do some setup
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
// connect and flush the request out
conn.connect();
conn.getOutputStream().flush();
// now fetch the results
String response = getResponse(conn);
where getResponse() looks something like this, in your case you are getting
a pile of binary data back you might want to change the StringBuffer to a byte array
and chunk the reads by a larger increment.
private String getResponseOrig(HttpURLConnection conn)
{
InputStream is = null;
try
{
is = conn.getInputStream();
// scoop up the reply from the server
int ch;
StringBuffer sb = new StringBuffer();
while( ( ch = is.read() ) != -1 ) {
sb.append( (char)ch );
}
return sb.toString();
}
catch(Exception e)
{
Log.e(TAG, "biffed it getting HTTPResponse");
}
finally
{
try {
if (is != null)
is.close();
} catch (Exception e) {}
}
return "";
}
As you are talking about image data which can be large, other things you need to be assiduous about in Android is making sure you release your memory as soon as you can, you've only got 16mb of heap to play with for all apps and it runs out fast and the GC will drive you nuts if you aren't really good about giving back memory resources

Categories

Resources