Image, Web, Remote, URL, URLConnection, BitmapFactory

Quickly share your Android Code Snippets here...

Image, Web, Remote, URL, URLConnection, BitmapFactory

Postby plusminus » Sat Oct 04, 2008 3:59 pm

This snippets loads a Image from the web:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public Bitmap getRemoteImage(final URL aURL) {
  2.  
  3.                 try {
  4.  
  5.                         final URLConnection conn = aURL.openConnection();
  6.  
  7.                         conn.connect();
  8.  
  9.                         final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
  10.  
  11.                         final Bitmap bm = BitmapFactory.decodeStream(bis);
  12.  
  13.                         bis.close();
  14.  
  15.                         return bm;
  16.  
  17.                 } catch (IOException e) {
  18.  
  19.                         Log.d("DEBUGTAG", "Oh noooz an error...");
  20.  
  21.                 }
  22.  
  23.                 return null;
  24.  
  25.         }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4
Last edited by plusminus on Thu Oct 09, 2008 1:28 pm, edited 1 time in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Re: Image, Web, Remote, URL, URLConnection, BitmapFactory

Postby mharkus » Thu Oct 09, 2008 11:29 am

plusminus wrote:This snippets loads a Image from the web:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public Bitmap getRemoteImage(final URL aURL) {
  2.                 try {
  3.                         final URLConnection conn = aURL.openConnection();
  4.                         conn.connect();
  5.                         final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
  6.                         final Bitmap bm = BitmapFactory.decodeStream(bis);
  7.                         bis.close();
  8.                         is.close();
  9.                         return bm;
  10.                 } catch (IOException e) {
  11.                         Log.d("DEBUGTAG", "Oh noooz an error...");
  12.                 }
  13.                 return null;
  14.         }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4



You can remove is.close() coz is doesn't exist
mharkus
Freshman
Freshman
 
Posts: 4
Joined: Thu Oct 09, 2008 11:22 am
Location: Philippines

Postby plusminus » Thu Oct 09, 2008 1:27 pm

Oops, yes. I was simplyfying the code a bit, before posting it and should have looked over it again.

Thx :)
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby alpesh_harsoda » Sat Oct 25, 2008 12:03 pm

hello plusminus,
when i run this code the exception caught unknown socket error -1
the conn.connect() not connected so what i have to do.....
User avatar
alpesh_harsoda
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat Sep 20, 2008 12:35 pm
Location: Rajkot,Gujarat(India)

Postby plusminus » Sat Oct 25, 2008 4:16 pm

alpesh_harsoda wrote:hello plusminus,
when i run this code the exception caught unknown socket error -1
the conn.connect() not connected so what i have to do.....


Add the ACCESS_INTERNET -Permission to your AndroidManifest.xml

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby alpesh_harsoda » Tue Nov 18, 2008 8:04 am

hi plusminus,
thnks for reply but i have already put that permission.although this error comes.

Thnks,:)
ALpesh
User avatar
alpesh_harsoda
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat Sep 20, 2008 12:35 pm
Location: Rajkot,Gujarat(India)

Top

Postby linkmaster_6 » Thu Mar 19, 2009 8:57 pm

Code works fine for me. Btw what do you think would be a good way to cache the image in case the user doesnt have itnernet access
linkmaster_6
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Thu Oct 16, 2008 8:52 pm

The code which you provide is doen't work for .jpg images

Postby manojkumar » Thu Apr 02, 2009 4:32 pm

linkmaster_6 wrote:Code works fine for me. Btw what do you think would be a good way to cache the image in case the user doesnt have itnernet access


Hi friends,

I could not able to load the .jpg images (300 * 300).

When I saw a log, it states that:

I/global ( 1220): Apr 2, 2009 6:13:08 PM java.io.BufferedInputStream <init>
I/global ( 1220): INFO: Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if a 8k buffer is required.
D/skia ( 1220): xxxxxxxxxxxxxx failure to skip request 5176 actual 1736
D/skia ( 1220): xxxxxxxxxxx jpeg error 91 Miscellaneous marker 0x%02x, length %u

The code which I have written is:

URL aURL = new URL(fotoUrl);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
///* Buffered is always good for a performance plus.
BufferedInputStream bis = new BufferedInputStream(is,512*1024);
//BufferedInputStream bis = new BufferedInputStream(is);
///* Decode url-data to a bitmap.

Bitmap b = BitmapFactory.decodeStream(bis);

Can any one please help me.
Manoj
manojkumar
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Apr 02, 2009 4:27 pm

Postby E-Nature » Sat Apr 17, 2010 10:08 pm

I would like to display the image in full screen (for example 1200x800 pixels big) so the user can scroll through the image.
but no matter what I set in the xml the image always gets resized to the device-screen.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:layout_width="500px"
  2.  
  3. android:layout_height="500px"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

or
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:layout_width="wrap_content"
  2.  
  3. android:layout_height="wrap_content"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

or
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:layout_width="fill_parent"
  2.  
  3. android:layout_height="fill_parent"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


how can I display it in full original size?
thank you :)
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Re: Image, Web, Remote, URL, URLConnection, BitmapFactory

Postby pabmadi » Wed Apr 21, 2010 1:15 pm

plusminus wrote:This snippets loads a Image from the web:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public Bitmap getRemoteImage(final URL aURL) {
  2.                 try {
  3.                         final URLConnection conn = aURL.openConnection();
  4.                         conn.connect();
  5.                         final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
  6.                         final Bitmap bm = BitmapFactory.decodeStream(bis);
  7.                         bis.close();
  8.                         return bm;
  9.                 } catch (IOException e) {
  10.                         Log.d("DEBUGTAG", "Oh noooz an error...");
  11.                 }
  12.                 return null;
  13.         }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


Hi, how can I use this snippet?
This call is Ok?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ImageView img =(ImageView)findViewById(R.id.icon);
  2. Bitmap drawable = getRemoteImage("http://www.generales2008.mir.es/99imgs/LOGO990026.gif");
  3. img.setImageDrawable(drawable);
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Thanks.-
pabmadi
Junior Developer
Junior Developer
 
Posts: 19
Joined: Thu Apr 08, 2010 9:25 pm

Postby TAKEphONE » Thu Apr 22, 2010 12:10 pm

Hi,

Does this work for ICO (windows icons) file, too ?

I am trying to show the "favicon" (aka "shortcut icon") of websites.

I have code that downloads them to the device, but BitmapFactory.decodeFile(filename) always returns null...

TIA
TAKEphONE
Junior Developer
Junior Developer
 
Posts: 20
Joined: Mon Nov 03, 2008 11:35 am

Re: Image, Web, Remote, URL, URLConnection, BitmapFactory

Postby himym » Mon May 31, 2010 5:04 pm

hey TAKEphONE,
I have the same problem as you. My Bitmap always gets assigned a null, and I just don't understand why.
Did you or anyone else solve that problem? Thanks!
himym
Developer
Developer
 
Posts: 33
Joined: Sun Oct 18, 2009 9:50 am

Re: Image, Web, Remote, URL, URLConnection, BitmapFactory

Postby rebill » Fri Aug 20, 2010 3:29 pm

Thanks for sharing,very nice.
rebill
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Aug 20, 2010 3:17 pm

Re: Image, Web, Remote, URL, URLConnection, BitmapFactory

Postby vani.v » Mon Aug 30, 2010 6:31 am

hi,
i want to be able to put many images and scroll through it and also be able to zoom it..i used viewflipper,in that i can swipe through images, but i can zoom only one image and cant swipe further.

Regards,
Vani
vani.v
Freshman
Freshman
 
Posts: 7
Joined: Fri Jul 30, 2010 11:59 am

Re: Image, Web, Remote, URL, URLConnection, BitmapFactory

Postby danizdeman » Thu Oct 07, 2010 6:28 pm

hi,
i have quite a similar problem though its entirely reversed..... i need to be able to upload the image to a URL (via FTP or it could be sent as a byte stream; once it gets to the server).... i am a total noob at this so any advice is welcome please.

thank you,
Dan Deman
danizdeman
Freshman
Freshman
 
Posts: 3
Joined: Sun Sep 05, 2010 5:43 am

Top
Next

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 4 guests