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();
5
  • Base64String Commented Jul 12, 2013 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. Commented Jul 12, 2013 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". Commented Jul 12, 2013 at 10:56
  • 1
    stackoverflow.com/questions/10191871/… check this here Commented Jul 12, 2013 at 10:58
  • isn't your problem solved? Commented Jul 12, 2013 at 13:38

4 Answers 4

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.....

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

Comments

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

Comments

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.

Comments

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.

Comments

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.