简体   繁体   中英

How to show Image programmatically in custom alert dialog

When you click on button camera should open After capturing the image it should show in customAlertDialog with two options save and cancel

here is my code:

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");
    }
}

}

Do this call from where you want to call camera::

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, 101);

When you come back after capturing picture from camera. It will call "onActivityResult()" and do code like this:

 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





        }
    }
}

Now create a XML layout name as "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>

And here you are done with whole code..and happy coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM