图片转换,字符串,字符串转换图片

imageview变成Bitmap对象

 photo.buildDrawingCache();
Bitmap bm = photo.getDrawingCache();

Bitmap变成字符串

aa=bitmapToBase64(bm);

bitmapToBase64方法

 public static String bitmapToBase64(Bitmap bitmap) {
         String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                 baos.flush();
                baos.close();
                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

字符串又变成Bitmap对象

Bitmap outphot=stringtoBitmap(aa);

stringtoBitmap方法

public static Bitmap stringtoBitmap(String string) {
// 将字符串转换成Bitmap类型
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
                    bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return bitmap;
    }

最后再变成ImageView

                outphoto.setImageBitmap(outphot);

项目刚刚用到,记下方便以后用

你可能感兴趣的:(app,imageview)