在Android中将图像从drawable转换为byte数组

时间:2015-03-17 04:51:35

标签: android image parse-platform bytearray

由于我将图像发送到Parse.com,我必须将其转换为字节数组。我的第一种方法是从库中选择一个图像并将其转换为字节数组,如下所示:

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

        }

上面的代码工作正常,图像成功存储解析。现在,当我没有选择图像时,应用程序崩溃了。显然是bcoz,没有数据被发送,并且引发了解析异常。

现在,我想设置一个默认图像,即在我的drawable文件夹中进行解析,以防从图库中选择任何图像,以便解析操作不会受到空数据的干扰。

我的方法是在起始时设置默认图像:

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

现在,我如何将此默认图像转换为ByteArray,以便可以将其发送到解析?

谢谢和问候

3 个答案:

答案 0 :(得分:3)

检查我的工作代码:

首先,您需要使用此方法将drawable图片转换为Bitmap

 public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    final int width = !drawable.getBounds().isEmpty() ? drawable
            .getBounds().width() : drawable.getIntrinsicWidth();

    final int height = !drawable.getBounds().isEmpty() ? drawable
            .getBounds().height() : drawable.getIntrinsicHeight();

    final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width,
            height <= 0 ? 1 : height, Bitmap.Config.ARGB_8888);

    Log.v("Bitmap width - Height :", width + " : " + height);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

获取位图对象后,需要使用。

将其转换为byte数组
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

答案 1 :(得分:1)

它可以帮助您转换drawable - &gt;的ByteArray ....

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

答案 2 :(得分:0)

此代码对我有用:

    Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.my_pic);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
相关问题