Byte[] to image

Put your problem here if it does not fit any of the other categories.

Byte[] to image

Postby DaltonGR » Tue Feb 22, 2011 12:30 pm

Good morning,
I have a .Net web service that return a byte[](actually its an image). How can i read this byte[] array and in code convert it to an image an display it in a ImageView.

thanks in advance.
DaltonGR
Junior Developer
Junior Developer
 
Posts: 13
Joined: Fri Jan 28, 2011 5:32 pm

Top

Re: Byte[] to image

Postby strider2023 » Mon Feb 28, 2011 11:16 am

use this code snippet...(initially i wrote it fr downloading songs...sry did not change it feeling lazy :mrgreen: )
just remember for file name pass 1.jpg or 1.png (whatever)...extension is important.

Code: Select all
/**
    * This function takes in two parameters.
    * @param songURL - The URL from where the song needs to be downloaded.
    * @param fileName - The file name to be given to  the song.
    * @param filePath - The file path to store the song.
    */
   public static void DownloadFromUrl(String songURL, String fileName, String filePath) { 
      //this is the downloader method
      try {
          URL url = new URL(songURL); //you can write here any link
          File file = new File(fileName);
          long startTime = System.currentTimeMillis();
          Log.d("SongManager", "download begining");
          Log.d("SongManager", "download url:" + url);
          Log.d("SongManager", "downloaded file name:" + fileName);

          /* Open a connection to that URL. */
          URLConnection ucon = url.openConnection();
          //int length = ucon.getContentLength();
          //ProteusFunctions.createToast("Length" + length, getBaseContext());
         
          /* Define InputStreams to read from the URLConnection. */
          InputStream is = ucon.getInputStream();
          BufferedInputStream bis = new BufferedInputStream(is);
          /* Convert the Bytes read to a String. */
         FileOutputStream fos = new FileOutputStream(filePath + file);

          /* Read bytes to the Buffer until there is nothing more to read(-1). */
          ByteArrayBuffer baf = new ByteArrayBuffer(50);
          int current = 0;
         
          while ((current = bis.read()) != -1) {
              baf.append((byte) current);
          }

         fos.write(baf.toByteArray());
         fos.close();
         Log.d("SongManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
      } catch (IOException e) {
         Log.d("SongManager", "Error: " + e);
      }
   }
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests