之前那样做,发现图片好模糊,加了一个压缩图片的,还有存储图片的方法如下:
大神的demo地址:http://blog.csdn.net/jdsjlzx/article/details/44160603#html
微信上传图片问题链接:http://blog.csdn.net/lmy0111ly/article/details/50586455#comments
- 照相机以及返回结果
//照相机
public void photo() {
File file = new File(Environment.getExternalStorageDirectory(), "/shopping/cache/");
if (!file.exists())
file.mkdirs();
filePath = Environment.getExternalStorageDirectory() + "/shopping/cache/";
fileName = String.valueOf(System.currentTimeMillis() + ".png");
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath, fileName)));
startActivityForResult(openCameraIntent, TAKE_PICTURE);
}
//照相机返回的图片
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
switch (requestCode) {
case TAKE_PICTURE:
if (Bimp.tempSelectBitmap.size() < 9 && resultCode == RESULT_OK) {
builder = new CustomDialogLoading.Builder(this, "正在处理图片,请稍后...");
mDialog = builder.create();
mDialog.show();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
path = filePath + fileName;
Bitmap bm = FileUtils.convertToBitmap(path, 480, 800);//大小压缩
String savePath = FileUtils.saveBitmap(bm, fileName);//质量压缩并存储
ImageItem takePhoto = new ImageItem();
takePhoto.setBitmap(bm);
takePhoto.setImagePath(savePath);
Bimp.tempSelectBitmap.add(takePhoto);
handler.sendEmptyMessage(0);
}
});
thread.start();
}
break;
}
}
- 压缩图片及存储图片方法
public class FileUtils {
/**
* 根据路径加载bitmap 将缩放后的bitmap返回去
*
* @param path 路径
* @param w 宽
* @param h 长
* @return
*/
public static Bitmap convertToBitmap(String path, int w, int h) {
try {
BitmapFactory.Options opts = new BitmapFactory.Options();
// 设置为ture只获取图片大小
opts.inJustDecodeBounds = true;
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
// 返回为空
BitmapFactory.decodeFile(path, opts);
int width = opts.outWidth;
int height = opts.outHeight;
float scaleWidth = 0.f, scaleHeight = 0.f;
if (width > w || height > h) {//如果宽度大于 传入的宽度 或者 高度大于 传入的高度大于
// 缩放
scaleWidth = ((float) width) / w;
scaleHeight = ((float) height) / h;
}
opts.inJustDecodeBounds = false;
//缩放后的高度和宽度取最大值
float scale = Math.max(scaleWidth, scaleHeight);
opts.inSampleSize = (int) scale;//此处是最后的宽高值
// //WeakReference 弱引用
// WeakReference<Bitmap> weak = new WeakReference<Bitmap>actory.decodeFile(path, opts));
// Bitmap bMapRotate = Bitmap.createBitmap(weak.get(BitmapF(), 0, 0, weak.get().getWidth(), weak.get().getHeight(), null, true);
Bitmap bMapRotate = BitmapFactory.decodeFile(path, opts);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bMapRotate.compress(Bitmap.CompressFormat.JPEG, 50, baos);
ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
Bitmap bitmap = BitmapFactory.decodeStream(bais);
System.out.println("质量 压缩---------------width-" + bMapRotate.getWidth() + "---height--" + bMapRotate.getHeight()+"------------------size---"+bMapRotate.getRowBytes());
if (bMapRotate != null) {
return bMapRotate;
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 存储图片,返回路径
* @param bm
* @param picName
* @return
*/
public static String saveBitmap(Bitmap bm, String picName) {
try {
if (!isFileExist(picName)) {
File tempf = createSDDir("");
}
File f = new File(SDPATH, picName);
if (f.exists()) {
f.delete();
}
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 70, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return SDPATH + picName;
}
}