3

I am facing one real issue. I need to convert image into byte array format, so that I can upload the byte array into web server. I have tried a lot however its not working. I am also getting negative values for byte array. I am not sure what i am doing wrong to take byte values in array.

Below is my code. Please help me what i am doing wrong?

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.home_menu_icon);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
user2576081
  • 39
  • 1
  • 1
  • 3
  • [Base64String](http://stackoverflow.com/a/16744086/2332217) – Oli Jul 12 '13 at 10:49
  • Don't convert to base64 ... If you care about quality try to compress to PNG format as JPEG is the worst possible: `bmp.compress(Bitmap.CompressFormat.PNG, 100, bos);` Otherwise the code code seems to be OK. – gunar Jul 12 '13 at 10:51
  • `The fact that primitives are signed in Java is irrelevant - A byte is merely 8 bits and whether you interpret that as a signed range or not is up to you. There is no magic flag to say "this is signed" or "this is unsigned".` – Selvin Jul 12 '13 at 10:56
  • 1
    http://stackoverflow.com/questions/10191871/converting-bitmap-to-bytearray-android check this here – Marko Niciforovic Jul 12 '13 at 10:58
  • isn't your problem solved? – Chintan Rathod Jul 12 '13 at 13:38

4 Answers4

1

Here i show code for both if your image is from GALLERY and CAMERA

if (requestCode == IMAGE_PICKER_REQUEST && resultCode == RESULT_OK) {
        fileName = getRealPathFromURI(data.getData());

        try {
             if (bitmap != null) {
                    bitmap.recycle();
                }
            InputStream stream = getContentResolver().openInputStream(
                    data.getData());

            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            image.setImageBitmap(bitmap);
            //picNameText.setText("Selected: en"
                //  + getStringNameFromRealPath(fileName));

            ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1);
            imageInByte = stream1.toByteArray();

        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        } 
    }

    if (requestCode == IMAGE_TAKER_REQUEST && resultCode == RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        image.setImageBitmap(photo);

        ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.PNG, 100, stream2);
        imageInByte = stream2.toByteArray();

    }

enjoy it work for me.....

Dhaval Patel
  • 694
  • 4
  • 12
0

Try following code.

int bytes = bitmap.getByteCount();
//Create a new buffer
ByteBuffer buffer = ByteBuffer.allocate(bytes); 
//Move the byte data to the buffer
b.copyPixelsToBuffer(buffer); 
byte[] array = buffer.array();
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
0
  1. A 32-bit Bitmap use a int32 to save a ARGB color, and we can use a int array to represent a bitmap. Using Bitmap.getPixels() and Bitmap.setPixels() to convert a Bitmap to int array and vise verse. And a int array can be easily convert to byte array.
  2. @Chintan Rathod also show a good solution by using Bitmap.copyPixelsToBuffer(), nearly the same with the first one.
  3. Since your goal is to upload the bitmap to server, send a compressed file is the best solution. Your code is just doing the right thing. You said that you are getting negative values in byte array, which is not an error. Just upload the byte array to server and save it as a jpeg file.
faylon
  • 7,360
  • 1
  • 30
  • 28
0

A 32-bit Bitmap use a int32 to save a ARGB color, and we can use a int array to represent a bitmap. Using Bitmap.getPixels() and Bitmap.setPixels() to convert a Bitmap to int array and vise verse. And a int array can be easily convert to byte array.