因为使用的imageloader加载图片:防止oom,可以进行二级缓存,不用担心我们的内存,所以获取的是图片的地址。
打开系统相册简单:传参的时候有传:文件路径:返回的data就是空的,但是full图片(也就是原始的图片:区别于缩略图)会在文件路径生成的;
如果没传文件路径:返回的就是缩略图。
注意:三星手机的图片问题
详细代码:内存控制和文件存放调整完的最终版本
//获取原始图片的角度
private int getBitmapDegree(String path) {
int degree = 0;
try {
// 从指定路径下读取图片,并获取其EXIF信息
ExifInterface exifInterface = new ExifInterface(path);
// 获取图片的旋转信息
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
Log.e(“jxf”,”orientation”+orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
/**
* 获取磁盘缓存路径
* @param name 文件夹名称
* @return 缓存路径
*/
private File getDiskCacheDir(Context context, String name) {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
File file = new File(context.getExternalCacheDir(), name);
if (!file.exists())
file.mkdirs();
return file;
} else {
File file = new File(context.getCacheDir(), name);
if (!file.exists())
file.mkdirs();
return file;
}
}
/**
* 保存图片到系统相册
* @param image
*/
private Uri saveImageToDICM(Context context, File image) {
Uri uri = null;
// 保存到相册
try {
String uriString = MediaStore.Images.Media.insertImage(
context.getContentResolver(), image.getAbsolutePath(),
image.getName(), image.getName());
uri = Uri.parse(uriString);
// // 通知系统相册刷新
// context.sendBroadcast(new Intent(
// Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(image)));
} catch (FileNotFoundException e) {
Log.e(“jxf”,”捕捉异常FileNotFoundException”);
}
return uri;
}
/**开启相机*/
case R.id.btn_take_photo:
//防止内存溢出
Log.e("jxf", "ImageLoader清理内存缓存");
ImageLoader.getInstance().clearMemoryCache();
Intent intent5 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent5.putExtra(MediaStore.EXTRA_OUTPUT, getOutputPhoto());
startActivityForResult(intent5, REQUEST_CODE_IMAGE_CAPTURE);
menuWindow.dismiss();
//照相返回的
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
/**拍照*/
case REQUEST_CODE_IMAGE_CAPTURE:
if (resultCode == RESULT_OK) {
// uri =saveImageToDICM(getApplicationContext(),
// outPutPhoto);
// if (uri == null) {
// return;
// }
// Log.e(“jxf”,”打印保存照片地址uri”+uri.toString());
Log.e(“jxf”,”打印保存照片地址path”+outPutPhoto.getAbsolutePath());
int degree=getBitmapDegree(outPutPhoto.getAbsolutePath());
Log.e(“jxf”,”打印旋转尺寸”+degree);
//使用大图做尺寸压缩
BitmapFactory.Options options = new BitmapFactory.Options();
// // 从解码器中获取原始图片的宽高,这样避免了直接申请内存空间
// options.inJustDecodeBounds = true;
// Calculate inSampleSize
options.inSampleSize = 5;
// // 压缩完后便可以将inJustDecodeBounds设置为false了。
// options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
//做尺寸压缩
Log.e(“jxf”,”做尺寸压缩”);
Bitmap bitmap = BitmapFactory.decodeFile(outPutPhoto.getAbsolutePath(), options);
Log.e(“jxf”,”bitmap大小打印”+(bitmap.getRowBytes() * bitmap.getHeight()));
// 根据旋转角度,生成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Log.e(“jxf”, “做角度变换了”);
Bitmap returnBm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
Log.e(“jxf”,”returnBm大小打印”+(returnBm.getRowBytes() * returnBm.getHeight()));
//压缩进原来的文件中
File imagePath = new File(getCacheDir(), “” + Calendar.getInstance().getTimeInMillis());// /sdcard/aijingxi/cropImage
File parentFile = imagePath.getParentFile();
if (!parentFile.exists()) {// 判断上级目录是否存在,不存在就需要创建
parentFile.mkdirs();
}
// 把Bitmap对象持久化到本地
OutputStream os = null;
try {
os = new FileOutputStream(imagePath);
returnBm.compress(Bitmap.CompressFormat.JPEG, 100, os);
Log.e(“jxf”, “做质量压缩压缩进硬盘”);
os.flush();
os.close();
} catch (FileNotFoundException e) {
Log.e(“jxf”,”切图类1–bitmap图片压缩时,报FileNotFoundException异常”);
e.printStackTrace();
} catch (IOException e) {
Log.e(“jxf”,”切图类1–bitmap图片压缩时,报IOException异常”);
e.printStackTrace();
}
//释放
if (bitmap != null && !bitmap.isRecycled()) {
// 回收并且置为null
bitmap.recycle();
bitmap = null;
Log.e(“jxf”, “bitmap回收”);
}
System.gc();
//释放
if (returnBm != null && !returnBm.isRecycled()) {
// 回收并且置为null
returnBm.recycle();
returnBm = null;
Log.e(“jxf”, “returnBm回收”);
}
System.gc();
uri =saveImageToDICM(getApplicationContext(),
imagePath);
Intent intent = new Intent(this, CropActivity.class);
intent.putExtra(“uri”, uri.toString());
Log.e(“jxf”,”传递的uri==”+ uri.toString());
startActivityForResult(intent, CROP_REQUEST_CODE);
}
break;
有管照相机的一些网站:有自己写caream的、有打开系统的
http://blog.csdn.net/dinglin_87/article/details/7917473 (重点)
http://cowboy.1988.blog.163.com/blog/static/75105798201422645818776/
http://www.cnblogs.com/franksunny/archive/2011/11/17/2252926.html
http://blog.csdn.net/jianghuiquan/article/details/8569187 (好文)