Download image from server

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Download image from server

Postby Nemat » Wed Oct 01, 2008 11:44 am

Hi,

I have to download an image from server.But I got an error "D/skia ( 197): xxxxxxxxxxx jpeg error 53 Not a JPEG file: starts with 0x%02x 0x%02x"

I have to perform two continues Http connection.First is used to get server response and second to get image from the server.First task is performed correctly but while downloading image I got the given eror.So the problem in getting image from server.

My code is:
Code: Select all
public ImageView getView(String myImageURL) {
       Log.i("TAG","downloadFile000000000000000000"+myImageURL);
        ImageView i = new ImageView(this);

        try {
                /* Open a new URL and get the InputStream to load data from it. */
                URL aURL = new URL(myImageURL);
                URLConnection conn = aURL.openConnection();
                Log.i("TAG","downloadFile1111111111111111111"+myImageURL);
                conn.connect();
                InputStream is = conn.getInputStream();
                Log.i("TAG","downloadFile2222222222222222222222"+is.read());
                /* Buffered is always good for a performance plus. */
                BufferedInputStream bis = new BufferedInputStream(is);
                Log.i("TAG","downloadFile33333333333333333333="+bis.read());
                /* Decode url-data to a bitmap. */
               // Bitmap bm = BitmapFactory.decodeStream(bis);
                Bitmap bm = BitmapFactory.decodeStream(is);
               // Log.i("TAG","downloadFile4444444444444444444"+bm.getHeight());
               // bis.close();
                is.close();
                /* Apply the Bitmap to the ImageView that will be returned. */
                i.setImageBitmap(bm);
                Log.i("TAG","downloadFile555555555555555555555");
           } catch (IOException e) {
              Log.i("TAG","downloadFile666666666666666666666666");
                Log.e("DEBUGTAG", "Remtoe Image Exception", e);
           }
       
        /* Image should be scaled as width/height are set. */
       // i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        /* Set the Width/Height of the ImageView. */
       
        return i;
    }


This code works fine when I performed this task alone in another project.
Nemat
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Thu Aug 21, 2008 3:16 pm
Location: Jaipur

Top

Re: Download image from server

Postby ninor » Wed Oct 01, 2008 12:12 pm

Nemat wrote:This code works fine when I performed this task alone in another project.


Have you added the necessary permissions to the AndroidManifest.xml in this new project?
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby Nemat » Thu Oct 02, 2008 4:11 pm

I have added all the required requests.I think the image is downloaded correctly but the problem is in converting the image into bitmap or displaying the bitmap.....
I dont know the actual reason.I think so becoz I got JPEG error.
Nemat
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Thu Aug 21, 2008 3:16 pm
Location: Jaipur

Postby ninor » Thu Oct 02, 2008 9:02 pm

Here's an example of downloading and displaying images from flickr.

http://code.google.com/p/apps-for-android/source/browse/trunk/Photostream/src/com/google/android/photostream/?r=73

(edited link)
Last edited by ninor on Fri Oct 03, 2008 3:19 pm, edited 2 times in total.
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby Nemat » Fri Oct 03, 2008 3:13 pm

Actually I have to download image from a server.
This example doesnt have any code for downloading image from server.
I've applied another way .I got image 2-3 times correctly after that it again starts giving error.
My new code is:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public Bitmap getFromServer(String url)
  3.  
  4.         {
  5.  
  6.                 Bitmap bitmap = null;
  7.  
  8.         InputStream in = null;
  9.  
  10.         OutputStream out = null;
  11.  
  12.         int IO_BUFFER_SIZE=1*1024*1024;
  13.  
  14.         ImageView i = new ImageView(this);
  15.  
  16.  
  17.  
  18.         try {
  19.  
  20.             in = new BufferedInputStream(new URL(url).openStream(),IO_BUFFER_SIZE);
  21.  
  22.  
  23.  
  24.             final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
  25.  
  26.             out = new BufferedOutputStream(dataStream, 4 * 1024);
  27.  
  28.             copy(in, out);
  29.  
  30.             out.flush();
  31.  
  32.  
  33.  
  34.             final byte[] data = dataStream.toByteArray();
  35.  
  36.             bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
  37.  
  38.            // i.setImageBitmap(bitmap);
  39.  
  40.         } catch (IOException e) {
  41.  
  42.             android.util.Log.e("IO", "Could not load buddy icon: "
  43.  
  44. + this, e);
  45.  
  46.         } finally {
  47.  
  48.             closeStream(in);
  49.  
  50.             closeStream(out);
  51.  
  52.         }
  53.  
  54.         return bitmap;
  55.  
  56.         }
  57.  
  58.        
  59.  
  60.         private static void copy(InputStream in, OutputStream out) throws
  61.  
  62.         IOException {
  63.  
  64.                 byte[] b = new byte[4 * 1024];
  65.  
  66.                 int read;
  67.  
  68.                 while ((read = in.read(b)) != -1) {
  69.  
  70.                     out.write(b, 0, read);
  71.  
  72.                 }
  73.  
  74.             }
  75.  
  76.  
  77.  
  78.             /**
  79.  
  80.              * Closes the specified stream.
  81.  
  82.              *
  83.  
  84.              * @param stream The stream to close.
  85.  
  86.              */
  87.  
  88.             private static void closeStream(Closeable stream) {
  89.  
  90.                 if (stream != null) {
  91.  
  92.                     try {
  93.  
  94.                         stream.close();
  95.  
  96.                     } catch (IOException e) {
  97.  
  98.                         android.util.Log.e("IO", "Could not close stream", e);
  99.  
  100.                     }
  101.  
  102.                 }
  103.  
  104.             }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


It works fine initially.I dont know what is the reason behind.It gives new error now : jpeg error 20 Improper call to JPEG library in state %d........
Nemat
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Thu Aug 21, 2008 3:16 pm
Location: Jaipur

Postby ninor » Fri Oct 03, 2008 3:22 pm

Sorry, I posted the address of one single .java instead of all the example project.

http://code.google.com/p/apps-for-android/source/browse/trunk/Photostream/src/com/google/android/photostream/?r=73

And let's see if someone catches the error in your code!
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Top

Postby plusminus » Fri Oct 03, 2008 3:25 pm

Hi,

downloading images from the web is a part of my :rarrow: :src: Google Weather Tutorial :larrow:

I use the same code for my OpenStreetMapView and never had a single problem using it. (Works with all, PNG and JPEG)

Best 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

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 6 guests