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;
}
3
  • 1
    check your imports Commented Mar 31, 2017 at 11:04
  • Please post your error logs, thanks. Commented Mar 31, 2017 at 11:04
  • Welcome to StackOverflow! Please read the user guidelines on how to ask a good question before posting a question (stackoverflow.com/help/how-to-ask) Thank You Commented Mar 31, 2017 at 11:04

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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();
}

3 Comments

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.
so can you explain me Mr greenapps how i can do that with explaination
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
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();

5 Comments

getting this error prntscr.com/eqpmom while I am using code as prntscr.com/eqpn24
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. As OP is trying to do. Better help him.
Are u sure file exist inside your device.
I am getting it from server link
You need to first download the image then convert into byteArray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.