-1

I am working on ImageView in which I want to set an image into Imageview but that Image(byte array) is coming directly from web server.. The concept is:

1) I am sending various parameteres to web server and in return the web server is sending me an image of that particular ID.(Just an Image, not the URL)

2) Now I want to show that Image directly into ImageView without saving into the SD card, so that whenever the user wants to see any other ID related Image, the Image should come directly from server and we are not saving it at our end.

[Editted]

Fetching value from server and return it in byte[]

     static BufferedReader in=null;

byte[] result_image;
          DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(uri1);
        if(list!=null)
        {

        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
        httpPost.setEntity(formEntity);

        }
         //URI uri=httpPost.getURI();
        HttpResponse httpResponse = httpClient.execute(httpPost);
        in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
       // String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
              sb.append(line +" "); //sb.append(line +NL);
        }
        in.close();



       result_image = String.valueOf(sb).getBytes();

}
    catch(UnsupportedEncodingException e)
    {
        String err = (e.getMessage()==null)?"Cant connect to server":e.getMessage();
        Log.e("Network Error:",err); 
    }
    catch (MalformedURLException e) {
        String err = (e.getMessage()==null)?"Malformed Exception":e.getMessage();
        Log.e("Malformed Exception:",err); 

     } 
     catch(Exception ex)
     {
        // Log.i("Exception,ex", ex.getMessage());
         String err = (ex.getMessage()==null)?"NetworkConnectionException":ex.getMessage();
         Log.e("NetworkConnectionException:",err); 
     }
    finally {

        if (in != null) {
            try {
                    in.close();
             } catch (Exception ex) {
                 String err = (ex.getMessage()==null)?"Excepion":ex.getMessage();
                 Log.e("Exception:",err); 
            }
        }

Here the result_image is a byte [] and using this byte[] am decoding it and shwing it in Bitmap..Please help... Any help into this is really appreciated.. Thanks..

Kanika
  • 10,648
  • 18
  • 61
  • 81

6 Answers6

3

you can use like this

URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
imageView.setImageBitmap(myBitmap);
Zaz Gmy
  • 4,236
  • 3
  • 19
  • 30
0

Try this:

Bitmap bMap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
image.setImageBitmap(bMap);
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

Given that you are loading from the net you probably want to lazy load the images. There are many examples of this. Though many of them save the images to the sd card, you will have to adapt and store the images into some sort of memory structure.

Community
  • 1
  • 1
Jim Baca
  • 6,112
  • 2
  • 24
  • 30
0

First of all, you can not set url as DataSource of ImageView. You have to have get response from URL and convert into byte.

Second if you don't want to save the image on to SDCARD then simple assign the byte array in ImageView, here you are not saving the image on to SDCARD. The byte array will remain till your app is active in RAM.

The code is as below. Here URL points to an image something like http://domain/test.jpg

URL myFileUrl =null;          
        try {
             myFileUrl= new URL(url);
        } catch (MalformedURLException e) {
            return false;
        }
        try {
             HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
             conn.setInstanceFollowRedirects(false);
             conn.setDoInput(true);
             conn.connect();                       
             InputStream is = conn.getInputStream();
             Bitmap bitmap = BitmapFactory.decodeStream(is);
             image.setImageBitmap(bitmap);
             return true;
        } catch (IOException e) {
            System.out.println("error="+e.getMessage());
            e.printStackTrace();        
            return false;
        }
Maneesh
  • 6,098
  • 5
  • 36
  • 55
0

The decodeByteArray() method is the suggested way to do this. If its not working for you, You can decode it manually like the following code.

URL yourl = new URL(src);
HttpURLConnection connection = (HttpURLConnection) yourl.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();                                
bis = new BufferedInputStream(input,int);                   
baf = new ByteArrayBuffer(int);

int current = 0;                        
while ((current = bis.read()) != -1) {                  
baf.append((byte) current);
}

Hope the above code shows some alternate ideas to you. Happy coding.

Kartihkraj Duraisamy
  • 3,171
  • 2
  • 25
  • 36
0
if we use  asynctask then like this 
//declare Bitmap 
//new Loading().execute("http://Hsot/123.jpg"); //Call the asynctask

protected Bitmap doInBackground(String... args)
 {         
 bitmap=BitmapFactory.decodeStream((InputStream)newURL(args[0]).getContent());
 return bitmap;
}
 protected void onPostExecute(Bitmap image) 
{
     my_image.setImageBitmap(image);
     my_image2.setImageBitmap(image);
}
sandhu
  • 305
  • 4
  • 4