Android 图片传输到服务器与取回
2016-04-27 09:38
369 查看
1、将图片转换成字节数组,再转换成字符串
2、从服务器取回图片的字符串数据时,转换字节数组,再转换成bitmap
public Bitmap getBitmap(){
Bitmap bitmap = null;
try{
byte[] pictureBitmap = Base64.decode(picture,Base64.DEFAULT);<span style="white-space:pre"> </span>//picture为返回的图片字符串
for(int i=0; i<pictureBitmap.length; i++) {
if (pictureBitmap[i] < 0) {
pictureBitmap[i] += 256;
}
}
bitmap = BitmapFactory.decodeByteArray(pictureBitmap, 0, pictureBitmap.length);
}catch(Exception e){
e.printStackTrace();
}
return bitmap;
}
private String getBitmapStr(Bitmap bitmap){ String bitmapStr = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); byteArrayOutputStream.close(); byte[] buffer = byteArrayOutputStream.toByteArray(); bitmapStr = Base64.encodeToString(buffer,0,buffer.length,Base64.DEFAULT); } catch (IOException e) { e.printStackTrace(); } return bitmapStr; }
2、从服务器取回图片的字符串数据时,转换字节数组,再转换成bitmap
public Bitmap getBitmap(){
Bitmap bitmap = null;
try{
byte[] pictureBitmap = Base64.decode(picture,Base64.DEFAULT);<span style="white-space:pre"> </span>//picture为返回的图片字符串
for(int i=0; i<pictureBitmap.length; i++) {
if (pictureBitmap[i] < 0) {
pictureBitmap[i] += 256;
}
}
bitmap = BitmapFactory.decodeByteArray(pictureBitmap, 0, pictureBitmap.length);
}catch(Exception e){
e.printStackTrace();
}
return bitmap;
}
相关文章推荐
- 带动画的新手指导页
- Android 使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚
- Android控件
- Android自动滚动 轮播循环的ViewPager
- Android API之Assets下文件操作
- Android学习路线总结
- Android 桌面未读
- android EditText实现圆弧背景
- Android EditText的inputType属性值
- 第一天写博客,就写写android的屏幕适配吧
- android activity 管理器AMS----概述
- BLE蓝牙在Android开发中的应用(二)
- android清除指定堆栈中某个activity
- SVN:MAC SVN Android studio不能提交.so文件
- Android五种数据传递方法汇总
- android源码分析之View的事件分发(上)
- android中的通信机制总结
- android sdk content loader 0%
- Mac下Android Studio环境搭建
- Android 开发值得订阅的日报和周刊