public static void startCamera(Fragment fragment){
File file=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+
File.separator+"user_icon.jpg");
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//intent隐式调用启动拍照界面
intent.putExtra("return-data",false);//该属性设置为false表示拍照后不会将数据返回到onResluet方法中(建议设置为false,这样获取的图片会比较清晰)
ComponentName componentName=intent.resolveActivity(fragment.getContext().getPackageManager());
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//该属性设置的是拍照后图片保存的位置
//防止app启动意图时崩溃
if (componentName!=null){
fragment.startActivityForResult(intent,Variable.request_camera_code);
}
}
public static void startPhoto(Fragment fragment){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);//intent隐式调用启动相册界面
intent.setType("image/*");//设置数据类型
ComponentName componentName = intent.resolveActivity(fragment.getContext().getPackageManager());
Log.d("tag", "startPhoto: "+componentName);
if (componentName != null) {//防止启动意图时app崩溃
fragment.startActivityForResult(intent, Variable.request_photo);
}
}
public static void cropphoto(Fragment fragment, Uri uri){
//设置裁剪图片保存位置
File bomb=new File(fragment.getContext().getExternalCacheDir(),"bmob");
Log.d("tag", "cropphoto: "+bomb);
if (!bomb.exists()){
bomb.mkdir();
}
File file=new File(bomb,"user_icon.jpg");
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Intent intent=new Intent("com.android.camera.action.CROP");//intent隐式调用启动拍照界面
intent.setDataAndType(uri,"image/*");//设置需要裁剪的图片地址
intent.putExtra("crop", "true");//通过put(key,value)方法设置相关属相
intent.putExtra("aspectX", 1);//设置图片宽高比例
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 240);//设置图片宽高
intent.putExtra("outputY", 240);
intent.putExtra("return-data", false);//该属性设置为false表示拍照后不会将数据返回到onResluet方法中(建议设置为false,这样获取的图片会比较清晰)
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//该属性设置的是拍照后图片保存的位置
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//设置输出格式
intent.putExtra("noFaceDetection", true);//是否取消人脸识别
/*ComponentName componentName = intent.resolveActivity(context.getPackageManager());
Log.d("TAG", "cropphoto: "+componentName);
if (componentName!=null){
fragment.startActivityForResult(intent,Variable.request_crop);
}*/
fragment.startActivityForResult(intent,Variable.request_crop);
}
- 重写OnActivityResult方法获取并设置对应数据
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case Variable.request_camera_code://当返回的请求码是启动拍照时设置的,此时调用裁剪方法
Assist.cropphoto(RegisterFragment.this, Uri.fromFile(new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"user_icon.jpg")));
break;
case Variable.request_photo://当返回的请求码是启动相册时设置的,此时获取图片uri并调用裁剪方法
Assist.cropphoto(RegisterFragment.this,data.getData());
break;
case Variable.request_crop://当返回的请求码是启动裁剪时设置的,此时便可获取最终裁剪好的图片
Operation op=new Operation(context);
String path=context.getExternalCacheDir()+
File.separator+"bmob"+File.separator+"user_icon.jpg";//裁剪好的图片保存位置
Bitmap icon=op.decodeBitmap(path);//加载本地图片,并获取大小合适的bitmap
if (icon!=null){
user_icon.setImageBitmap(icon);//将获取的图片设置到imagerview
bmobfile=new BmobFile(new File(path));
bmobfile.uploadblock(new UploadFileListener() {
@Override
public void done(BmobException e) {
if (e==null){
//bmob上传图片成功
}
}
});
}
break;
}
}