0

I am new to android and I need to save image from url into db and fetch it from db. I an converting image from link into byte array but getting the following error:

Cannot Resolve ByteArrayBuffer

This is the code:

 private byte[] getLogoImage(String url){
    try {
             URL imageUrl = new URL(url);
             URLConnection ucon = imageUrl.openConnection();

             InputStream is = ucon.getInputStream();
             BufferedInputStream bis = new BufferedInputStream(is);

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

             return baf.toByteArray();
     } catch (Exception e) {
             Log.d("ImageManager", "Error: " + e.toString());
     }
     return null;
}
Thiago Elias
  • 939
  • 1
  • 6
  • 21
Dheeraj Singh
  • 21
  • 1
  • 5

3 Answers3

1

Update your method getLogoImage() as below.

Use AsyncTask and call this method from doInBackground().

Here is the working code. Try this:

private byte[] getLogoImage(String url) {

    try {

        URL imageUrl = new URL(url);
        URLConnection ucon = imageUrl.openConnection();

        InputStream is = ucon.getInputStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int read = 0;

        while ((read = is.read(buffer, 0, buffer.length)) != -1) {
            baos.write(buffer, 0, read);
        }

        baos.flush();

        return  baos.toByteArray();

    } catch (Exception e) {
        Log.d("ImageManager", "Error: " + e.toString());
    }

    return null;
}
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
0

Try like this

Bitmap bm = null;
   InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);
       byte[] imageArray = getBytes(bm);

and after this use below method

  public static byte[] getBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
    return stream.toByteArray();
}
Nikhil Sharma
  • 593
  • 7
  • 23
  • Pretty bad code to convert the received bytes to a bitmap first. Memory problems. No need to do so as you can put the received bytes directly in a byte array. Or write them to a byte array output stream. – greenapps Mar 31 '17 at 11:27
  • so can you explain me Mr greenapps how i can do that with explaination – Nikhil Sharma Mar 31 '17 at 11:31
  • and suppose if i want to compress my image first thn i want to get byte array how i can do that explain me that also – Nikhil Sharma Mar 31 '17 at 11:32
0

Use This

BitmapFactory.Options options = null;
            options = new BitmapFactory.Options();
            options.inSampleSize = 3;
            Bitmap bitmap = BitmapFactory.decodeFile(url,
                    options);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
            byte[] byte_arr = stream.toByteArray();
Rajkumar Kumawat
  • 290
  • 2
  • 11