public class MeFragment extends BaseFragment implements View.OnClickListener {
private String TAG = getClass().getSimpleName();
private ImageView ivPhoto;
private static final int CAMERA = 1;// 拍照
private static final int GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
private final String IMAGE_FILE_LOCATION = Environment.getExternalStorageDirectory() + "/temp.jpg"; //temp file
private AlertDialog dialog;
private View dialogView;
private View backgroundView;
private boolean isBackground;
public static MeFragment newInstance(String param1, String param2) {
MeFragment fragment = new MeFragment();
Bundle args = new Bundle();
return fragment;
}
@Override
protected void initView() {
backgroundView=bind(R.id.ivPhotoBg);
ivPhoto = bind(R.id.ivPhoto);
dialogView=LayoutInflater.from(getActivity()).inflate(R.layout.dialog_select_image,null);
String uri = ShareUtils.getPhotoUri();
String uriBackground=ShareUtils.getPhotoBackground();
if(!TextUtils.isEmpty(uri)){
ivPhoto.setImageURI(Uri.parse(uri));
}
if(!TextUtils.isEmpty(uriBackground)){
setBackground(Uri.parse(uriBackground));
}
dialog = new AlertDialog.Builder(getActivity(), R.style.fullScreen).create();
initListener(this,R.id.ivPhoto,R.id.ivPhotoBg);
initListener(this,dialogView.findViewById(R.id.btCamera),dialogView.findViewById(R.id.btPhoto),dialogView.findViewById(R.id.btCancel));
}
@Override
public int getRootLayoutId() {
return R.layout.fragment_me;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btCamera:
setPhotoIntent(CAMERA);
break;
case R.id.btPhoto:
setPhotoIntent(GALLERY);
break;
case R.id.ivPhoto:
isBackground=false;
dialog.setCancelable(true);
dialog.show();
dialog.setContentView(dialogView);
break;
case R.id.ivPhotoBg:
isBackground=true;
dialog.setCancelable(true);
dialog.show();
dialog.setContentView(dialogView);
break;
}
}
/**
* 为选择的模式设置intent参数,调用系统相机 系统相册 系统裁剪
* @param type GALLERY CAMERA PHOTO_REQUEST_CUT
* */
private void setPhotoIntent(int type) {
Intent intent = new Intent();
switch (type) {
case PHOTO_REQUEST_CUT:
break;
case GALLERY:
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, GALLERY);
break;
case CAMERA:
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera, CAMERA);
break;
}
}
/**
* 调用系统裁剪功能:
* 传入图片uri
* @param uri picture uri
*/
private void startPhotoZoom(Uri uri) {
Log.i(TAG, " url================" + uri);
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", 600);
intent.putExtra("outputY", 600);
intent.putExtra("return-data", true);
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, String.format("request code = %d result code = %d data is null %b", requestCode, resultCode, data == null));
if(dialog!=null){
dialog.dismiss();
}
if (resultCode != Activity.RESULT_OK || data == null) {
Toast.makeText(getActivity(), "you have cancel option", Toast.LENGTH_SHORT).show();
return;
}
switch (requestCode) {
case CAMERA:// 当选择拍照时调用
if(isBackground){
setBackground(data.getData());
}else{
startPhotoZoom(data.getData());//相册返回uri丢给系统裁剪
}
break;
case GALLERY:// 当选择相册时
if(isBackground){
setBackground(data.getData());
Log.i(TAG,data.getDataString());// uri content://com.android.providers.media.documents/document/image%3A32507
ShareUtils.setPhotoBackground(data.getDataString());
}else{
startPhotoZoom(data.getData());//相册返回uri丢给系统裁剪
}
break;
case PHOTO_REQUEST_CUT:// 返回的结果
Bitmap bitmap = data.getParcelableExtra("data");//裁剪后,取出bitmap界面显示
ShareUtils.setPhotoUri(IMAGE_FILE_LOCATION);
saveBitmap(bitmap);
ivPhoto.setImageBitmap(bitmap);
break;
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void setBackground(Uri uri) {
InputStream inputStream;
Drawable drawable=null;
try {
Log.i(TAG," uri "+uri);
inputStream = getActivity().getContentResolver().openInputStream(uri);
drawable=Drawable.createFromStream(inputStream,"dataString");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(drawable!=null){
backgroundView.setBackground(drawable);
Log.d(TAG,"set ok");
}else{
Log.e(TAG,"setBackground error");
}
}
/**
* 把头像保存在本地
* @param bm
* @return
*/
private Uri saveBitmap(Bitmap bm) {
File tmpDir = new File(IMAGE_FILE_LOCATION);
tmpDir.delete();
if (!tmpDir.getParentFile().exists()) {
tmpDir.mkdir();
}
File img = new File(IMAGE_FILE_LOCATION);
try {
FileOutputStream fos = new FileOutputStream(img);
bm.compress(Bitmap.CompressFormat.PNG, 85, fos);
fos.flush();
fos.close();
Toast.makeText(getActivity(), "保存成功了", Toast.LENGTH_SHORT).show();
return Uri.fromFile(img);
} catch (FileNotFoundException e) {
Toast.makeText(getActivity(), "保存失败", Toast.LENGTH_SHORT).show();
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "保存失败", Toast.LENGTH_SHORT).show();
return null;
}
}
/**
* 1、bitmap to uri
* <p/>
* Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null));
* <p/>
* 2、uri to bitmap
* <p/>
* Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
* 读取图片属性:旋转的角度
*
* @param path 图片绝对路径
* @return degree旋转的角度
*/
public static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
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 angle
* @param bitmap
* @return Bitmap
*/
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
//旋转图片 动作
Matrix matrix = new Matrix();
matrix.postRotate(angle);
System.out.println("angle2=" + angle);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizedBitmap;
}
}
android 调用原生相机
最新推荐文章于 2024-04-22 09:36:13 发布