保存图片小的一般操作步骤:
1. 调用系统相机
Intent intent = newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,1);
2. 保存照片
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
System.out.println("onActivityResult start");
if (resultCode == Activity.RESULT_OK) {
System.out.println("enter");
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
Log.i("TestFile",
"SD card is not avaiable/writeable right now.");
System.out.println("SD card is not avaiable/writeable right now.");
return;
}
String name = System.currentTimeMillis() + ".jpg";;
//Toast.makeText(this, name, Toast.LENGTH_LONG).show();
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
FileOutputStream b = null;
File file = new File("/mnt/sdcard/DCIM/Camera/");
file.mkdirs();// 创建文件夹
System.out.println("mkdirs");
String fileName = "/mnt/sdcard/DCIM/Camera/"+name;
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
小图片的造成的原因,从返回值中取照片的数据是已经被压缩了,要想不被压缩我们可以在调用系统相机时指定照片的保存位置
private String camera_path = Environment.getExternalStorageDirectory().toString() + "/Photo_LJ/";//照片保存位置
private String camera_photo_name;// 保存的名称
Intent intent = newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
File path1= newFile(camera_path);if (!path1.exists()) {
path1.mkdirs();
}
camera_photo_name= System.currentTimeMillis() + ".jpg";
File file= newFile(path1, camera_photo_name);//mOutPutFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent,1);
重载方法
@Overrideprotected void onActivityResult(int requestCode, intresultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);switch(requestCode) {caseTAKE_PICTURE:if (resultCode ==RESULT_OK) String filename = camera_path + "/" +camera_photo_name;
Bitmap bm=compressImageFromFile(filename);
ImageItem takePhoto= newImageItem();
takePhoto.setBitmap(bm);
Bimp.tempSelectBitmap.add(takePhoto);
}break;
}
照片很大,要显示出来我们最好对它进行一下压缩
privateBitmap compressImageFromFile(String srcPath) {
BitmapFactory.Options newOpts= newBitmapFactory.Options();
newOpts.inJustDecodeBounds= false;//只读边,不读内容
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, null);
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 = Config.ARGB_8888;//该模式是默认的,可不设
newOpts.inPurgeable = true;//同时设置才会有效
newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收
bitmap=BitmapFactory.decodeFile(srcPath, newOpts);//return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩//其实是无效的,大家尽管尝试
returnbitmap;
}
好了,可以到照片保存的位置检查一下了,已经是2M左右大小的照片了。
原文:http://www.cnblogs.com/wicrecend/p/4865154.html