Downloading Image from internet

All your problems with Audio, Video and Images.

Downloading Image from internet

Postby Menor » Mon May 16, 2011 6:01 pm

I'm trying to download images from web...

Code: Select all
try {
               url = new java.net.URL(url + image);
               } catch (MalformedURLException e) {
                  e.printStackTrace();
               }
               Bitmap imageJustDownloaded = getRemoteImage(url);
               downloadedImages.add(getRemoteImage(url));




Code: Select all
public Bitmap getRemoteImage(URL url) {
      
      Bitmap bmImg;
             try {
                HttpURLConnection conn= (HttpURLConnection)url.openConnection();
                  conn.setDoInput(true);
                  conn.connect();
                  int length = conn.getContentLength();
                  InputStream is = conn.getInputStream();
                  bmImg = BitmapFactory.decodeStream(is);
                  return bmImg;
             } catch (IOException e) {
                  e.printStackTrace();
             }
             return null;
        }


Sometimes all images are downloaded, but once in awhile some images aren't downloaded.

Does anyone know why this happen?
Menor
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Mon Apr 11, 2011 3:45 pm

Top

Re: Downloading Image from internet

Postby Menor » Tue May 17, 2011 12:52 pm

I changed my code, now just one image is downloaded (the first one)...

Take a look at my code:
Code: Select all
public Bitmap getRemoteImage(String fileName) {
      
      Bitmap bmImg;
        try {   
               java.net.URL url = new java.net.URL(DataManager.getInstance().getURL() + fileName);
                URLConnection conn = url.openConnection();
                conn.connect();
                BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
                Bitmap bm = BitmapFactory.decodeStream(bis);
                bis.close();
                conn.setConnectTimeout(0);
                System.out.println ("BMP: " + bm);
                System.out.println ("URL: " + url.toString());
                return bm;
               
        } catch (IOException e) {
               System.out.println(e);
        }
        return null;
   }


This method must to download 4 images, the call of this method is:

Code: Select all
for (int i=0; i<array.size(); i++){   
         String imageFileName = array.get(i).getImage();
         downloadedImages.add(getRemoteImage(imageFileName));
}


Anyone can help me?
Menor
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Mon Apr 11, 2011 3:45 pm

Re: Downloading Image from internet

Postby Menor » Wed May 18, 2011 3:42 pm

I was testing this app on Samsung Galaxy Tab...

On Samsung Nexus S this problem doesn't happen... On emulator, i'd tryed on several configurations and all images were loaded, anyone knows why it happen on "galaxy tab" device?

Thanks.
Menor
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Mon Apr 11, 2011 3:45 pm

Re: Downloading Image from internet

Postby kpbird » Fri May 20, 2011 5:53 am

Hello

Android : How to download image from server http://goo.gl/ho9Fs

you can find simplest way to download image from server
kpbird
Freshman
Freshman
 
Posts: 9
Joined: Fri Oct 03, 2008 1:40 pm

Re: Downloading Image from internet

Postby Menor » Fri May 20, 2011 1:15 pm

Hi kpbird... Thanks for answer...

I already found a solution to my problem yesterday afternoon, but thanks for your answear, it can help another people...

To contribute to forum, i'm going to show my download code:

Code: Select all
public class DownloadImageFromWeb {
   
   public DownloadImageFromWeb(){
      
   }
   
   public Bitmap getImageFromWeb (String fileName){
      int IO_BUFFER_SIZE = 4;
      Bitmap bitmap = null;
       InputStream in = null;
       BufferedOutputStream out = null;

       try {
           in = new BufferedInputStream(new URL("http://www.MYURL.com/" + fileName).openStream(), IO_BUFFER_SIZE);

           final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
           out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
           CopyStream(in, out);
           out.flush();

           final byte[] data = dataStream.toByteArray();
           BitmapFactory.Options options = new BitmapFactory.Options();
           //options.inSampleSize = 1;

           bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
       } catch (IOException e) {
          bitmap = null;
       } finally {
          if (bitmap == null){
             return null;
             
          }
           try {
            in.close();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
           try {
            out.close();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
         
       }
       return bitmap;
   }
   
   private void CopyStream(InputStream is, OutputStream os)
    {
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            for(;;)
            {
              int count=is.read(bytes, 0, buffer_size);
              if(count==-1)
                  break;
              os.write(bytes, 0, count);
            }
        }
        catch(Exception ex){}
    }

}

Menor
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Mon Apr 11, 2011 3:45 pm

Re: Downloading Image from internet

Postby krishjlk » Fri May 20, 2011 1:24 pm

how to convert the bitmap image to jpeg ?????
plz post mr , menor
/*
A good programmer is the one who always looks both ways b4 crossing a one-way street
*/
User avatar
krishjlk
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Fri May 20, 2011 6:09 am
Location: KK , @Ahmedabad, Gujrath, India

Top

Re: Downloading Image from internet

Postby Menor » Sat May 21, 2011 5:43 pm

Sorry, i don't know how to do it...
Menor
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Mon Apr 11, 2011 3:45 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 15 guests