3

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

3
  • First you need to convert your image to Bitmap and then convert it to byte array. Commented Mar 17, 2015 at 4:55
  • Can you explain with the code? Commented Mar 17, 2015 at 4:56
  • how you are picking an image from gallery ? Commented Mar 17, 2015 at 4:58

2 Answers 2

5

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

2 Comments

Where do I set the Resource Id of the drawable.?
First get your drawable using Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher); and then pass to this in the method like Bitmap bitmap = drawableToBitmap(drawable);
2

It will help you to convert drawable -- > ByteArray....

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();

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.