问题描述
- android byte转图片的问题
- ```public class MainActivity extends Activity {
private ImageView iv1iv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
//获取iv1的图片并将图片转成byte
Bitmap bitmap = ((BitmapDrawable)iv1.getDrawable()).getBitmap();ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG 100 baos);
byte[] byteArray = baos.toByteArray();
String sendString;
try {
//将byte转为String
sendString = new String(byteArray UTF-8"" );
try {
//将String转回byte
byte[] data = sendString.getBytes(""UTF-8"");
// 为UTF8编吗
// 把二进制图片转成位图
YuvImage yuvimage = new YuvImage(data ImageFormat.NV21 100
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
// 80--JPG图片的质量[0-100]100最高
yuvimage.compressToJpeg(new Rect(0 0 20 20) 80 baos2);
byte[] jdata = baos2.toByteArray();
Bitmap bitmap2 = BitmapFactory.decodeByteArray(jdata 0
jdata.length);
iv2.setImageBitmap(bitmap2);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}} catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }}private void initview() { // TODO Auto-generated method stub iv1=(ImageView) findViewById(R.id.imageView1); iv2=(ImageView) findViewById(R.id.imageView2);}
}
解决方案
问题已解决
String stra = d.getResult().getImage(); byte[] data = stra.getBytes(""ISO-8859-1""); Bitmap bitmap = BitmapFactory.decodeByteArray(data 0 data.length); image.setImageBitmap(bitmap);
String转byte要使用ISO-8859-1编码格式,UTF-8不行
解决方案二:
Android 中 -Java中byte[]转String问题
Android 中 -Java中byte[]转String问题
Android 中 -Java中byte[]转String问题