编码和解码图像

时间:2014-03-04 04:44:26

标签: android json encoding decoding

我想使用json将图像插入'MYSQL'数据库并将插入的图像反转为'ImageView'。我完成编码以及插入图像但不能进行解码并将图像设置为'ImageView'。 / p>

    //Encoding the imageView
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.id.imageVieweditprofileuserphoto);
        BitmapDrawable drawable = (BitmapDrawable) imageofuser.getDrawable();
        bitmap = drawable.getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,90, stream); //compress to which format you want.
        byte [] byte_arr = stream.toByteArray();
        String image_str= Base64.encodeToString(byte_arr, 1);

//Image after encoding at server side(Database MySql)
        iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAIAAABt+uBvAAAAA3NCSVQICAjb4U/gAAAQPElEQVR4
        nO1c6W9cR3Kv6u735s0Mh4dFUjy0luUj9lo+13YQJIGdYAUs8ilAPuRPyJ+XAPFugBjYrLO2s+s1
        1rZsyxJ18BDP4XDueWcflQ/9ZjjiIUp8b0jCqx9IDTVHv+rfVFVXV1c9rFar8AzHg523ABcdzwg6
        Ac8IOgHPCDoBzwg6Ac8IOgHP

//Decoding the imageView
         imageofuser=(ImageView)view.findViewById(R.id.imageVieweditprofileuserphoto);
         byte[] imagefromserver=Base64.decode(Utility.userimage, Base64.NO_WRAP);
         Bitmap bmp=BitmapFactory.decodeByteArray(imagefromserver, 0, imagefromserver.length);
         imageofuser.setImageBitmap(bmp);

0 个答案:

没有答案