android 图片转Base64压缩上传
2016-08-18 11:32
549 查看
首先得到图片路径 photoPath,
String base64Str = Bitmap2StrByBase64(compressImageFromFile(photoPath));
private Bitmap compressImageFromFile(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;//只读边,不读内容
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = 800f;//
float ww = 480f;//
int be = 1;
if (w > h && w > ww) {
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//设置采样率
newOpts.inPreferredConfig = Bitmap.Config.ARGB_8888;//该模式是默认的,可不设
newOpts.inPurgeable = true;// 同时设置才会有效
newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
// return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩
//其实是无效的,大家尽管尝试
return bitmap;
}
public String Bitmap2StrByBase64(Bitmap bit){
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.JPEG, 40, bos);//参数100表示不压缩
byte[] bytes=bos.toByteArray();
bos.reset(); // TODO 有的图片本身不大压缩后反而变大, 暂未解决, 貌似没个卵用
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
String base64Str = Bitmap2StrByBase64(compressImageFromFile(photoPath));
private Bitmap compressImageFromFile(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;//只读边,不读内容
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = 800f;//
float ww = 480f;//
int be = 1;
if (w > h && w > ww) {
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//设置采样率
newOpts.inPreferredConfig = Bitmap.Config.ARGB_8888;//该模式是默认的,可不设
newOpts.inPurgeable = true;// 同时设置才会有效
newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
// return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩
//其实是无效的,大家尽管尝试
return bitmap;
}
public String Bitmap2StrByBase64(Bitmap bit){
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.JPEG, 40, bos);//参数100表示不压缩
byte[] bytes=bos.toByteArray();
bos.reset(); // TODO 有的图片本身不大压缩后反而变大, 暂未解决, 貌似没个卵用
return Base64.encodeToString(bytes, Base64.DEFAULT);
}
相关文章推荐
- react-native-image-picker在Android上闪退的解决办法(上传头像base64,压缩上传图片)
- Android上传头像,图片剪裁,压缩图片
- android通过Base64往服务器上传图片和对象
- Base64实现android端图片上传到服务器端
- Android-图片选择,压缩,上传,加载 的实例(图片上传(多图也可),服务器端实现,图片加载(图片轮播实现))
- 图片压缩,Base64编码后上传服务器
- Android上传头像,图片剪裁,压缩图片
- android处理图片工具类,图片压缩,把图片互换转换成base64和bitmap
- Android如何压缩图片上传服务端
- android 拍照或者图库选择 压缩后 图片 上传
- Android-图片选择,压缩,上传,加载的实例 (图片选择,压缩实现 )
- android 类似于微博拍照上传,压缩图片,避免oom
- 图片压缩上传 Android
- android 上传图片前压缩文件
- android通过Base64往服务器上传图片和对象
- Android的图片压缩并上传
- Android 通过Base64上传图片到服务器
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- Android 通过Base64上传图片到服务器