/**
*调用系统相机
*/
public voidstartCamera() {
Intent intent =newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
picPath= Contants.PicCachePath+ System.currentTimeMillis() +".jpg";
//加载路径
Uri uri = Uri.fromFile(newFile(picPath));
//指定存储路径,这样就可以保存原图了
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
//拍照返回图片
startActivityForResult(intent,CAMERA_QUERY_CODE);
}
/**
*调用相册
*/
public voidstartPhotos() {
Intent intent =newIntent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
Intent wrapperIntent = Intent.createChooser(intent, null);
try{
startActivityForResult(wrapperIntent,PHOTOS_QUERY_CODE);
}catch(android.content.ActivityNotFoundException e1) {
e1.printStackTrace();
ToastAlone.showToast(this,"打开手机相册失败!",Toast.LENGTH_LONG);
}
}
@Override
protected voidonActivityResult(intrequestCode, intresultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if(CAMERA_QUERY_CODE== requestCode) {
FileInputStream inputStream =null;
try{
inputStream =newFileInputStream(picPath);
//把流解析成bitmap
imagePicBitmap= BitmapFactory.decodeStream(inputStream);
imagePicBitmap=rotateBitmapByDegree(imagePicBitmap,getBitmapDegree(picPath));
//设置图片
if(null!=imagePicBitmap&& !imagePicBitmap.isRecycled()) {
saveImage(imagePicBitmap,2);
if(listBitmaps.size() >0) {
listBitmaps.add(Utils.comp(imagePicBitmap));
}
if(null!=touTiaoPictureAdapter) {
touTiaoPictureAdapter.notifyDataSetChanged();
}else{
touTiaoPictureAdapter=newTouTiaoPictureAdapter(this,listBitmaps);
recyclerView.setAdapter(touTiaoPictureAdapter);
}
}
}catch(Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(null!=imagePicBitmap&& !imagePicBitmap.isRecycled()) {
imagePicBitmap.recycle();
}
}
}
//调用相册返回
if(PHOTOS_QUERY_CODE== requestCode && resultCode ==RESULT_OK&&null!= data) {
//根据URI获取图片路径
picPath= Contants.PicCachePath+ System.currentTimeMillis() +".jpg";
try{
Uri selectedImage = data.getData();
String[] filePathColumns = {
MediaStore.Images.Media.DATA
};
ContentResolver resolver = getContentResolver();
imagePicBitmap= MediaStore.Images.Media.getBitmap(resolver,selectedImage);
if(null!=imagePicBitmap&& !imagePicBitmap.isRecycled()) {
String picturepath= UriUtils.getPath(this,selectedImage);
imagePicBitmap=rotateBitmapByDegree(imagePicBitmap,getBitmapDegree(picturepath));
saveImage(imagePicBitmap,2);
if(listBitmaps.size() >0) {
listBitmaps.add(Utils.comp(imagePicBitmap));
}
if(null!=touTiaoPictureAdapter) {
touTiaoPictureAdapter.notifyDataSetChanged();
}else{
touTiaoPictureAdapter=newTouTiaoPictureAdapter(this,listBitmaps);
recyclerView.setAdapter(touTiaoPictureAdapter);
}
}else{
Cursor cursor =this.getContentResolver().query(selectedImage,filePathColumns,
null, null, null);
if(null!= cursor && cursor.getCount() >0) {
cursor.moveToFirst();
intcolumnIndex = cursor.getColumnIndex(filePathColumns[0]);
//获取图片路径
String picturePath = cursor.getString(columnIndex);
cursor.close();
imagePicBitmap= ImageLoader.getInstance().loadImageSync("file://"+ picturePath);
imagePicBitmap=rotateBitmapByDegree(imagePicBitmap,getBitmapDegree(picturePath));
if(listBitmaps.size() >0) {
listBitmaps.add(imagePicBitmap);
}
if(null!=touTiaoPictureAdapter) {
touTiaoPictureAdapter.notifyDataSetChanged();
}else{
touTiaoPictureAdapter=newTouTiaoPictureAdapter(this,listBitmaps);
recyclerView.setAdapter(touTiaoPictureAdapter);
}
picPath= picturePath;
saveImage(imagePicBitmap,2);
}else{
ToastAlone.showToast(this,"选择图片失败",
Toast.LENGTH_LONG);
}
}
}catch(Exception e) {
e.printStackTrace();
ToastAlone.showToast(this,"选择图片失败",Toast.LENGTH_LONG);
}
}
}
三星手机图片旋转问题:
/**
*读取图片的旋转的角度
*
*@parampath图片绝对路径
*@return图片的旋转角度
*/
private intgetBitmapDegree(String path) {
intdegree =0;
try{
//从指定路径下读取图片,并获取其EXIF信息
ExifInterface exifInterface =newExifInterface(path);
//获取图片的旋转信息
intorientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch(orientation) {
caseExifInterface.ORIENTATION_ROTATE_90:
degree =90;
break;
caseExifInterface.ORIENTATION_ROTATE_180:
degree =180;
break;
caseExifInterface.ORIENTATION_ROTATE_270:
degree =270;
break;
}
}catch(IOException e) {
e.printStackTrace();
}
returndegree;
}
/**
*将图片按照某个角度进行旋转
*
*@parambm
*需要旋转的图片
*@paramdegree
*旋转角度
*@return旋转后的图片
*/
public staticBitmaprotateBitmapByDegree(Bitmap bm, intdegree) {
Bitmap returnBm =null;
//根据旋转角度,生成旋转矩阵
Matrix matrix =newMatrix();
matrix.postRotate(degree);
try{
//将原始图片按照旋转矩阵进行旋转,并得到新的图片
returnBm = Bitmap.createBitmap(bm,0,0,bm.getWidth(),bm.getHeight(),matrix, true);
}catch(OutOfMemoryError e) {
}
if(returnBm ==null) {
returnBm = bm;
}
if(bm != returnBm) {
bm.recycle();
}
returnreturnBm;
}
网友评论