对应点击头像 底部弹出框 拍照 相册取消
https://blog.csdn.net/bbtianshi/article/details/81224736
这里对于一个工具类 对于的裁决 ,等对应简单封装,有待完善 优化 对于的扩展
下面撸代码 这对于的是一个工具类
public class CameraGallaryUtil {
private static final int RESULT_OK = -1;
public static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
public static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
public static final int PHOTO_REQUEST_CUT = 3;// 裁剪结果
public static final Uri fileUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()
.getPath() + File.separator + "temp.jpg"));//临时储存的Uri
public static Bitmap getBitmapFromCG(Activity activity, int requestCode, int resultCode, Intent data) {
Bitmap bitmap = null;
switch (requestCode) {
// 如果是拍照
case PHOTO_REQUEST_TAKEPHOTO:
if (resultCode == RESULT_OK) {
// 没有指定特定存储路径的时候,data不为null
if (data != null) {
if (data.getData() != null) {
startPhotoZoom(activity, data.getData());
}
} else {
startPhotoZoom(activity, fileUri);
}
}
break;
// 如果是从相册选取
case PHOTO_REQUEST_GALLERY:
if (data != null) {
if (data.getData() != null) {
startPhotoZoom(activity, data.getData());
}
}
break;
//如果是裁剪完成
case PHOTO_REQUEST_CUT:
if (data != null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
bitmap = bundle.getParcelable("data");
}
}
break;
}
return bitmap;
}
private static void startPhotoZoom(Activity activity, Uri 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", 300);
intent.putExtra("aspectY", 400);
//outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 400);
intent.putExtra("return-data", true);
activity.startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
}
对于的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.lbb.rosewoodclient.mine.view.activity.CameraActivity">
<Button
android:id="@+id/camera"
android:text="拍照"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/photo"
android:text="相册"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/picture"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
最后就是对应的 主Activity
ublic class CameraActivity extends AppCompatActivity implements View.OnClickListener{
Button photo,camera;
ImageView picture;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
photo = (Button)findViewById(R.id.photo);
camera = (Button)findViewById(R.id.camera);
picture = (ImageView)findViewById(R.id.picture);
photo.setOnClickListener(this);
camera.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap=CameraGallaryUtil.getBitmapFromCG(this,requestCode,resultCode,data);
picture.setImageBitmap(bitmap);
}
@Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.camera:
// 利用系统自带的相机应用:拍照
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 此处这句intent的值设置关系到后面的onActivityResult中会进入那个分支,即关系到data是否为null
// 如果此处指定,则后来的data为null
// 只有指定路径才能获取原图
intent.putExtra(MediaStore.EXTRA_OUTPUT, CameraGallaryUtil.fileUri);
startActivityForResult(intent, CameraGallaryUtil.PHOTO_REQUEST_TAKEPHOTO);
break;
case R.id.photo:
intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, CameraGallaryUtil.PHOTO_REQUEST_GALLERY);
break;
}
}
}
简单实现 ,有待 完善 大神勿喷 ..............
对应7.0会有崩溃 下面给大家个简答暴力的方法
public class Myapp extends Application{ @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) @Override public void onCreate() { super.onCreate(); // android 7.0系统解决拍照的问题 StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); builder.detectFileUriExposure(); } }