1

I am trying to do something very simple that has ended up not so simple. I have a bitmap image and I have to convert it to bytes to send over a socket. The byte array sends over correctly ( I have checked), but when I convert back to a bitmap, I get bitmap=null results. I believe my error is in how I am converting my original bitmap to bytes, or when I try to turn my bytes back into a bitmap.

As I was debugging I realized that if I take my bitmap, convert it to bytes, and then convert it right back to a bitmap (without sending it over the socket) it is also null. Here is my code: How can I convert a bitmap to bytes, and then bytes back into a bitmap image?

        // I am getting my image from ApplicationInfo and I know I am getting it because I have opened it on my computer after extracting it
        Drawable icon = context.getPackageManager().getApplicationIcon(ai);
        Log.d("tag_name", "ICON" + icon);

        BitmapDrawable bitmapIcon = (BitmapDrawable)icon;
        Log.d("tag_name", "BITMAP Drawable" + bitmapIcon);

        // STREAM IMAGE DATA TO FILE
        // This is how I know I am correctly getting my png image (no errors here)

        FileOutputStream fosIcon = context.openFileOutput(applicationName + ".png", Context.MODE_PRIVATE);

        bitmapIcon.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, fosIcon);
        InputStream inputStream = context.openFileInput(applicationName + ".png");

        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        Log.d("tag_name", "BITMAP NAME" + bitmap);


        // get bitmap image in bytes to send

        int bytes = bitmap.getByteCount();
        Log.d("tag_name", "Number of Bytes" + bytes);

        ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
        bitmap.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

        byte[] array = buffer.array();
        Log.d("tag_name", "array" + array);
        int start=0;
        int len=array.length;
        Log.d("tag_name", "length" + len);

        if (len < 0)
            throw new IllegalArgumentException("Negative length not allowed");
        if (start < 0 || start >= array.length)
            throw new IndexOutOfBoundsException("Out of bounds: " + start);


        // Convert BACK TO bitmap (this will be done on the other side of my socket, but it is also null here???
        Bitmap bitmap_2 = BitmapFactory.decodeByteArray(array , 0, array.length);
        System.out.println("Bitmap Name 2" + bitmap_2);

        // Open SendToClient Class to send string and image over socket

        new SendToClient(array, applicationName, len, start, packagename).execute();
4
  • Why are you checking start and len for values which they cannot possibly have? Commented Apr 29, 2016 at 1:00
  • @EJP I had print statements for len just to make sure that everything was getting passed through my socket, which it is. If that is what you mean? Commented Apr 29, 2016 at 22:43
  • I mean you have int start = 0; and two lines you are checking that it isn't negative. You have int len = array.length; and two lines later you are checking that it isn't negative. start can't be negative, and neither can len, given these initializations. Commented May 1, 2016 at 21:52
  • @EJP Oh yes, I deleted that part. Commented May 2, 2016 at 19:13

1 Answer 1

3

I searched the internet and finally found someone with the same problem where bitmap returns null and here is how to fix it! For some reason it works to first convert byte array to jpeg, then to bitmap:

// Convert data to jpeg first then to bitmap (cant convert byte array directly to bitmap)
              YuvImage yuvimage=new YuvImage(data, ImageFormat.NV21, 100, 100, null);
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              yuvimage.compressToJpeg(new Rect(0, 0, 100, 100), 80, baos);
              byte[] jdata = baos.toByteArray();

              // Convert to Bitmap
              Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
              System.out.println("Bitmap Name 3" + bmp);

I added this code right under where I created my byte array. So the variable that I have here called "data" is by byte array.

Sign up to request clarification or add additional context in comments.

1 Comment

That's better than a null, however my bitmap is corrupted/glitched if I do it this way.

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.