/**
*調(diào)用系統(tǒng)相機(jī)
*/
public voidstartCamera() {
Intent intent =newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
picPath= Contants.PicCachePath+ System.currentTimeMillis() +".jpg";
//加載路徑
Uri uri = Uri.fromFile(newFile(picPath));
//指定存儲(chǔ)路徑俺陋,這樣就可以保存原圖了
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
//拍照返回圖片
startActivityForResult(intent,CAMERA_QUERY_CODE);
}
/**
*調(diào)用相冊(cè)
*/
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,"打開手機(jī)相冊(cè)失斦瓯酢瓢喉!",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));
//設(shè)置圖片
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();
}
}
}
//調(diào)用相冊(cè)返回
if(PHOTOS_QUERY_CODE== requestCode && resultCode ==RESULT_OK&&null!= data) {
//根據(jù)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);
}
}
}
三星手機(jī)圖片旋轉(zhuǎn)問題:
/**
*讀取圖片的旋轉(zhuǎn)的角度
*
*@parampath圖片絕對(duì)路徑
*@return圖片的旋轉(zhuǎn)角度
*/
private intgetBitmapDegree(String path) {
intdegree =0;
try{
//從指定路徑下讀取圖片奸鸯,并獲取其EXIF信息
ExifInterface exifInterface =newExifInterface(path);
//獲取圖片的旋轉(zhuǎn)信息
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;
}
/**
*將圖片按照某個(gè)角度進(jìn)行旋轉(zhuǎn)
*
*@parambm
*需要旋轉(zhuǎn)的圖片
*@paramdegree
*旋轉(zhuǎn)角度
*@return旋轉(zhuǎn)后的圖片
*/
public staticBitmaprotateBitmapByDegree(Bitmap bm, intdegree) {
Bitmap returnBm =null;
//根據(jù)旋轉(zhuǎn)角度苟鸯,生成旋轉(zhuǎn)矩陣
Matrix matrix =newMatrix();
matrix.postRotate(degree);
try{
//將原始圖片按照旋轉(zhuǎn)矩陣進(jìn)行旋轉(zhuǎn)幻林,并得到新的圖片
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;
}