private AlertDialog alertDialog;;
private final String path =Environment.getExternalStorageDirectory()+ "/image.png";
@OnClick(R.id.button1)
public void onViewClicked() {
View view=View.inflate(getContext(),R.layout.alter,null);
alertDialog = new AlertDialog.Builder(getContext()).setView(view).show();
Button but1=view.findViewById(R.id.but1);
Button but2=view.findViewById(R.id.but2);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//相机
Intent intent1=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent1.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(path)));
startActivityForResult(intent1,100);
alertDialog.dismiss();
}
});
//相册
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1=new Intent(Intent.ACTION_PICK);
intent1.setType("image/*");
startActivityForResult(intent1,1);
alertDialog.dismiss();
}
});
}**
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//相机
if (requestCode==100&&resultCode==RESULT_OK){
Intent intent1=new Intent("com.android.camera.action.CROP");
intent1.setDataAndType(Uri.fromFile(new File(path)),"image/*");
intent1.putExtra("CROP",true);
intent1.putExtra("aspectX",1);
intent1.putExtra("aspectY",1);
intent1.putExtra("outputX",100);
intent1.putExtra("outputY",100);
intent1.putExtra("return-data",true);
startActivityForResult(intent1,200);
}
//相册
if (requestCode==1&&resultCode==RESULT_OK){
Intent intent1=new Intent("com.android.camera.action.CROP");
Uri data1 = data.getData();
intent1.setDataAndType(data1,"image/*");
intent1.putExtra("aspectX",2);
intent1.putExtra("aspectY",2);
intent1.putExtra("outputX",200);
intent1.putExtra("outputY",200);
intent1.putExtra("return-data",true);
startActivityForResult(intent1,200);
}
//赋值
if (requestCode==200&&resultCode==RESULT_OK){
Bitmap bitmap=data.getParcelableExtra("data");
button1.setImageBitmap(bitmap);
}
}
}
相机相册
最新推荐文章于 2021-01-07 08:50:17 发布