BitMap、Drawable、InputStream、Byte[]交換


この方法は主にBitmap、Drawable、InputStream、Byte[]間の相互変換である.
/** * Bitmap   InputStream * @param bitmap * @return */
    public InputStream bitmapToInputStream(Bitmap bitmap){
        InputStream is;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        is = new ByteArrayInputStream(baos .toByteArray());
        return is;
    }


    /** * Bitmap   byte[] * @param is * @return */
    public byte[] bitmapToByteArray(InputStream is){
        byte[] data;
        Bitmap defaultIcon = BitmapFactory.decodeStream(is);
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);
          data = stream.toByteArray();
          return data;
    }

    /** * Drawable   byte[] * @param defaultIcon * @return */
    public byte[] drawableToByteArray(Bitmap defaultIcon){
          byte[] data;
          Drawable d = null; // the drawable (Captain Obvious, to the rescue!!!)
          Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);
          data = stream.toByteArray();
          return data;
    }  

    /** * byte   Bitmap * @param bitmapdata * @return */

    public Bitmap byteArrayToBitmap(byte[] bitmapdata){
        Bitmap bitmap =BitmapFactory.decodeByteArray(bitmapdata, 0,bitmapdata.length);
        return bitmap;
    }