单击按钮时相机应该打开捕获图像后应该在customAlertDialog中显示两个选项保存并取消
这是我的代码:
public class MainActivity extends AppCompatActivity{
Button photo;
static final int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
photo = (Button) findViewById(R.id.btn_photo);
photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
}
}
}
答案 0 :(得分:0)
从您想要调用camera ::
的地方拨打此电话 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 101);
从相机拍摄照片后再回来。它将调用" onActivityResult()"并做这样的代码:
public void onActivityResult(int requestcode,int resultcode,final Intent intent) {
super.onActivityResult(requestcode, resultcode, intent);
if (resultcode == RESULT_OK) {
if (requestcode == 101) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("Save", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Do nothing here. Because image is already saved but if you want to save it other place then do code.
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Do here code for for deleting image using uri
}
});
final AlertDialog dialog = builder.create();
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
dialog.setView(dialogLayout);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();
final ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
Bitmap photo = (Bitmap) intent.getExtras().get("data");
image.setImageBitmap(photo); //Edited here
}
}
}
现在创建一个XML布局名称为" go_pro_dialog_layout.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/goProDialogImage"
android:layout_width="300dp"
android:layout_height="300dp"
/>
</LinearLayout>
在这里,您完成了整个代码......以及快乐的编码。