2
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;  
          }
}
user1389233
  • 145
  • 2
  • 2
  • 9

3 Answers3

3

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!

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • 1- http://blog.sptechnolab.com/2011/03/04/android/android-load-image-from-url/ . 2- http://developer.android.com/training/displaying-bitmaps/index.html – Dheeresh Singh May 11 '12 at 11:27
  • InputStream is = connection.getInputStream(); the connection used in the above means the object of HttpURLConnection or the object of URLConnection – user1389233 May 12 '12 at 09:32
  • 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 – user1389233 May 12 '12 at 10:13
  • can you tell what error in detail ? if possible provide the log cat for same. – Dheeresh Singh May 14 '12 at 05:09
  • 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.downloadImage.downloadImage}: java.lang.NullPointerException 05-15 01:19:13.123: E/AndroidRuntime(542): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) – user1389233 May 15 '12 at 05:36
  • i have displayed the codings in above new block pls check whether its correct or not – user1389233 May 15 '12 at 05:45
  • what does that connection belongs to could you explain it ? InputStream is = connection.getInputStream(); – user1389233 May 15 '12 at 05:48
  • In this code HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); – Dheeresh Singh May 15 '12 at 07:08
  • hope so i ve tried probably correct ,Is ther any thing wrong with the above coding – user1389233 May 15 '12 at 10:31
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

Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

Look at this page and download the sample code. it will solve your problem

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

i leaf
  • 273
  • 7
  • 19