Search code examples
androidimageparse-platformarrays

Converting an image from drawable to byte array in Android


Since I am sending the image to Parse.com, I have to convert it into byte Array. My first approach is to select an image from gallery and convert it to byte array as follows:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
             mMediaUri = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(mMediaUri,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();

           // ImageView imageView = (ImageView) findViewById(R.id.imgView);
            propertyImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            Bitmap bmp = BitmapFactory.decodeFile(picturePath);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byteArray = stream.toByteArray();

        }

The above code works fine and the image is successfully stored to parse. Now, I when No image is selected, The App crashes. Obviously bcoz, no data is sent and parse exception is raised.

Now, I want to set a default image, that is in my drawable folder to go to parse, in case -no image is selected from the gallery, so that parse operations are not disturbed with null data.

My approach was to set the default image in the starting itself:

propertyImage=(ImageView)findViewById(R.id.imageViewOfImage);
        propertyImage.setImageResource(R.drawable.blank_image);

Now, how would I convert this default image to a ByteArray, so that it can be sent to parse?

Thanks and Regards


Solution

  • First you need to convert your Drawable image to Bitmap using this code written by Chris.Jenkins:

    public static Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
    
        // We ask for the bounds if they have been set as they would be most
        // correct, then we check we are  > 0
        final int width = !drawable.getBounds().isEmpty() ?
                drawable.getBounds().width() : drawable.getIntrinsicWidth();
    
        final int height = !drawable.getBounds().isEmpty() ?
                drawable.getBounds().height() : drawable.getIntrinsicHeight();
    
        // Now we check we are > 0
        final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width, height <= 0 ? 1 : height,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    
        return bitmap;
    }
    

    After getting your Bitmap object, you need to convert it to a byte array using this code from Mezm:

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    bmp.recycle();