Retriving image from server to android app

21,945

Solution 1

form : Load Large Image from server on Android

it is not uncommon for BitmapFactory.decodeFromStream() to give up and just return null when you connect it directly to the InputStream of a remote connection. Internally, if you did not provide a BufferedInputStream to the method, it will wrap the supplied stream in one with a buffer size of 16384. One option that sometimes works is to pass a BufferedInputStream with a larger buffer size like:

BufferedInputStream bis = new BufferedInputStream(is, 32 * 1024); A more universally effective method is to download the file completely first, and then decode the data like this:

InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 8190);

ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
    baf.append((byte)current);
}
byte[] imageData = baf.toByteArray();
BitmapFactory.decodeByteArray(imageData, 0, imageData.length);

FYI, the buffer sizes in this example are somewhat arbitrary. As has been said in other answers, it's a fantastic idea not to keep an image that size in memory longer than you have to. You might consider writing it directly to a file and displaying a downsampled version.

Hope that helps!

Solution 2

this may also help you

http://blog.sptechnolab.com/2011/03/04/android/android-load-image-from-url/

http://developer.android.com/training/displaying-bitmaps/index.html

Share:
21,945
user1389233
Author by

user1389233

Updated on December 07, 2020

Comments

  • user1389233
    user1389233 over 3 years
    package com.sample.downloadImage;
    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    import org.apache.http.util.ByteArrayBuffer;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    public class downloadImage extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
      Bitmap bitmap = DownloadImage("http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
    
    
            ImageView img = (ImageView) findViewById(R.id.img);
            img.setImageBitmap(bitmap);
        }
    
        private InputStream OpenHttpConnection(String urlString) 
        throws IOException
        {
            InputStream in = null;
            int response = -1;
    
            URL url = new URL(urlString); 
            URLConnection conn = url.openConnection();
    
            if (!(conn instanceof HttpURLConnection))                     
                throw new IOException("Not an HTTP connection");
    
            try{
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect();
                response = httpConn.getResponseCode();                 
                if (response == HttpURLConnection.HTTP_OK) {
                    in = httpConn.getInputStream();                                 
                }                     
            }
            catch (Exception ex)
            {
                throw new IOException("Error connecting");            
            }
            return in;     
        }
        private Bitmap DownloadImage(String URL)
        {        
            Bitmap bitmap = null;
            InputStream in = null;  
    
    
    
            try {
                in = OpenHttpConnection(URL);
                BufferedInputStream bis = new BufferedInputStream(in, 8190);
    
                ByteArrayBuffer baf = new ByteArrayBuffer(50);
                int current = 0;
                while ((current = bis.read()) != -1) 
                {
                    baf.append((byte)current);
                }
                byte[] imageData = baf.toByteArray();
                bitmap =BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
                in.close();
            } 
           catch (IOException e1) 
           {
    
                e1.printStackTrace();
            }
            return bitmap;                
        }
    }
    

    wanna retrive images from server , so i tried to post a image in server and retrive through url but it works good for small images and when it comes for big image more than 60kb , could some one give a idea to solve the problem

    package com.sample.downloadImage;
    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    import org.apache.http.util.ByteArrayBuffer;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    public class downloadImage extends Activity {
    
        HttpURLConnection httpConn;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
      Bitmap bitmap = DownloadImage("http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
    
    
            ImageView img = (ImageView) findViewById(R.id.img);
            img.setImageBitmap(bitmap);
        }
    
        private InputStream OpenHttpConnection(String urlString) 
        throws IOException
        {
            InputStream in = null;
            int response = -1;
    
            URL url = new URL(urlString); 
            URLConnection conn = url.openConnection();
    
            if (!(conn instanceof HttpURLConnection))                     
                throw new IOException("Not an HTTP connection");
    
            try{
                 httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect();
                response = httpConn.getResponseCode();                 
                if (response == HttpURLConnection.HTTP_OK) {
                    in = httpConn.getInputStream();  
    
                    DownloadImage(urlString);
                }                     
            }
            catch (Exception ex)
            {
                throw new IOException("Error connecting");            
            }
            return in;     
        }
    
        private Bitmap DownloadImage(String URL)
        {        
            Bitmap bitmap = null;
            //InputStream is = null;  
            InputStream in;
            try
            {
                in = httpConn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(in, 3 *1024);
                ByteArrayBuffer baf = new ByteArrayBuffer(50);
                int current = 0;
                while ((current = bis.read()) != -1)
                {
                    baf.append((byte)current);
                    byte[] imageData = baf.toByteArray();
                    bitmap =BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
                    return bitmap;     
                }
            }
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return bitmap;  
              }
    }
    
  • Dheeresh Singh
    Dheeresh Singh almost 12 years
  • user1389233
    user1389233 almost 12 years
    InputStream is = connection.getInputStream(); the connection used in the above means the object of HttpURLConnection or the object of URLConnection
  • user1389233
    user1389233 almost 12 years
    could you give me some other possible link to learn about the loading large size image in secure manner
  • user1389233
    user1389233 almost 12 years
    i have tried with above links those supports small images but the i am held up with some bigger size images
  • user1389233
    user1389233 almost 12 years
    i am getting an error at this place when i go for debug , " InputStream is = connection.getInputStream(); " wher should i pass the url in this section
  • Dheeresh Singh
    Dheeresh Singh almost 12 years
    can you tell what error in detail ? if possible provide the log cat for same.
  • user1389233
    user1389233 almost 12 years
    05-15 01:19:13.104: D/AndroidRuntime(542): Shutting down VM 05-15 01:19:13.104: W/dalvikvm(542): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 05-15 01:19:13.123: E/AndroidRuntime(542): FATAL EXCEPTION: main 05-15 01:19:13.123: E/AndroidRuntime(542): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.downloadImage/com.sample.downloadIm‌​age.downloadImage}: java.lang.NullPointerException 05-15 01:19:13.123: E/AndroidRuntime(542): at android.app.ActivityThread.performLaunchActivity(ActivityThr‌​ead.java:2663)
  • user1389233
    user1389233 almost 12 years
    i have displayed the codings in above new block pls check whether its correct or not
  • user1389233
    user1389233 almost 12 years
    what does that connection belongs to could you explain it ? InputStream is = connection.getInputStream();
  • Dheeresh Singh
    Dheeresh Singh almost 12 years
    In this code HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
  • user1389233
    user1389233 almost 12 years
    hope so i ve tried probably correct ,Is ther any thing wrong with the above coding