How to pause code for results from AlertDialog












0















When I run an AlertDialog asking the user to get a photo from the camera or the gallery the program doesn't seem to wait for the results and continues to execute. This causes the result to not get saved properly into the Image View field. Ignore the unused variables as I am not done coding this activity yet. I am new to this so any other criticism is appreciated.



package ca.android.whitehead.mycardswallet;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Objects;

public class AddEditCardActivity extends AppCompatActivity implements View.OnClickListener {

private EditText etCardName;
private ImageView ivCardFront, ivCardBack, ivBarcode;
private Button btnCardFront, btnCardBack, btnBarcode;

private Bitmap image;

private static final int SELECT_PHOTO = 1;
private static final int CAPTUR_PHOTO = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit_card);

etCardName = findViewById(R.id.etCardName);
ivCardFront = findViewById(R.id.ivCardFront);
ivCardBack = findViewById(R.id.ivCardBack);
ivBarcode = findViewById(R.id.ivBarcode);
btnCardFront = findViewById(R.id.btnCardFront);
btnCardBack = findViewById(R.id.btnCardBack);
btnBarcode = findViewById(R.id.btnBarcode);

btnCardFront.setOnClickListener(this);
btnCardBack.setOnClickListener(this);
}

@Override
public void onClick(View v)
{
switch (v.getId()){
case R.id.btnCardFront:
getImage();
if (image != null)
{
ivCardFront.setImageBitmap(image);
}
break;
case R.id.btnCardBack:
getImage();
if (image != null)
{
ivCardBack.setImageBitmap(image);
}
break;

}
}

public void getImage()
{
AlertDialog.Builder builder = new AlertDialog.Builder(AddEditCardActivity.this);
builder.setTitle("Pick from gallery or take new picture");
Toast.makeText(this, "In Get Image", Toast.LENGTH_SHORT).show();

builder.setItems(R.array.uploadImage, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent;
switch (which) {
case 0:
intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECT_PHOTO);
break;

case 1:
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTUR_PHOTO);
break;
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK) {
if (resultData != null) {
// this is the image selected by the user
Uri imageUri = resultData.getData();

if (imageUri != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(imageUri);
image = BitmapFactory.decodeStream(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
if (requestCode == CAPTUR_PHOTO && resultCode == RESULT_OK) {
if (resultData != null) {
// this is the image selected by the user
image = (Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data");
}
}
}


}










share|improve this question























  • Possible duplicate of Android: wait on user input from dialog?

    – 0X0nosugar
    Jan 2 at 22:10











  • So basically you should put the if (image != null){...} as next statement after image = ... .

    – 0X0nosugar
    Jan 2 at 22:14











  • Both show() on an AlertDialog and startActivityForResult() are asynchronous. Make use of the image if and when you get one, in onActivityResult().

    – CommonsWare
    Jan 2 at 22:15
















0















When I run an AlertDialog asking the user to get a photo from the camera or the gallery the program doesn't seem to wait for the results and continues to execute. This causes the result to not get saved properly into the Image View field. Ignore the unused variables as I am not done coding this activity yet. I am new to this so any other criticism is appreciated.



package ca.android.whitehead.mycardswallet;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Objects;

public class AddEditCardActivity extends AppCompatActivity implements View.OnClickListener {

private EditText etCardName;
private ImageView ivCardFront, ivCardBack, ivBarcode;
private Button btnCardFront, btnCardBack, btnBarcode;

private Bitmap image;

private static final int SELECT_PHOTO = 1;
private static final int CAPTUR_PHOTO = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit_card);

etCardName = findViewById(R.id.etCardName);
ivCardFront = findViewById(R.id.ivCardFront);
ivCardBack = findViewById(R.id.ivCardBack);
ivBarcode = findViewById(R.id.ivBarcode);
btnCardFront = findViewById(R.id.btnCardFront);
btnCardBack = findViewById(R.id.btnCardBack);
btnBarcode = findViewById(R.id.btnBarcode);

btnCardFront.setOnClickListener(this);
btnCardBack.setOnClickListener(this);
}

@Override
public void onClick(View v)
{
switch (v.getId()){
case R.id.btnCardFront:
getImage();
if (image != null)
{
ivCardFront.setImageBitmap(image);
}
break;
case R.id.btnCardBack:
getImage();
if (image != null)
{
ivCardBack.setImageBitmap(image);
}
break;

}
}

public void getImage()
{
AlertDialog.Builder builder = new AlertDialog.Builder(AddEditCardActivity.this);
builder.setTitle("Pick from gallery or take new picture");
Toast.makeText(this, "In Get Image", Toast.LENGTH_SHORT).show();

builder.setItems(R.array.uploadImage, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent;
switch (which) {
case 0:
intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECT_PHOTO);
break;

case 1:
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTUR_PHOTO);
break;
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK) {
if (resultData != null) {
// this is the image selected by the user
Uri imageUri = resultData.getData();

if (imageUri != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(imageUri);
image = BitmapFactory.decodeStream(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
if (requestCode == CAPTUR_PHOTO && resultCode == RESULT_OK) {
if (resultData != null) {
// this is the image selected by the user
image = (Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data");
}
}
}


}










share|improve this question























  • Possible duplicate of Android: wait on user input from dialog?

    – 0X0nosugar
    Jan 2 at 22:10











  • So basically you should put the if (image != null){...} as next statement after image = ... .

    – 0X0nosugar
    Jan 2 at 22:14











  • Both show() on an AlertDialog and startActivityForResult() are asynchronous. Make use of the image if and when you get one, in onActivityResult().

    – CommonsWare
    Jan 2 at 22:15














0












0








0








When I run an AlertDialog asking the user to get a photo from the camera or the gallery the program doesn't seem to wait for the results and continues to execute. This causes the result to not get saved properly into the Image View field. Ignore the unused variables as I am not done coding this activity yet. I am new to this so any other criticism is appreciated.



package ca.android.whitehead.mycardswallet;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Objects;

public class AddEditCardActivity extends AppCompatActivity implements View.OnClickListener {

private EditText etCardName;
private ImageView ivCardFront, ivCardBack, ivBarcode;
private Button btnCardFront, btnCardBack, btnBarcode;

private Bitmap image;

private static final int SELECT_PHOTO = 1;
private static final int CAPTUR_PHOTO = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit_card);

etCardName = findViewById(R.id.etCardName);
ivCardFront = findViewById(R.id.ivCardFront);
ivCardBack = findViewById(R.id.ivCardBack);
ivBarcode = findViewById(R.id.ivBarcode);
btnCardFront = findViewById(R.id.btnCardFront);
btnCardBack = findViewById(R.id.btnCardBack);
btnBarcode = findViewById(R.id.btnBarcode);

btnCardFront.setOnClickListener(this);
btnCardBack.setOnClickListener(this);
}

@Override
public void onClick(View v)
{
switch (v.getId()){
case R.id.btnCardFront:
getImage();
if (image != null)
{
ivCardFront.setImageBitmap(image);
}
break;
case R.id.btnCardBack:
getImage();
if (image != null)
{
ivCardBack.setImageBitmap(image);
}
break;

}
}

public void getImage()
{
AlertDialog.Builder builder = new AlertDialog.Builder(AddEditCardActivity.this);
builder.setTitle("Pick from gallery or take new picture");
Toast.makeText(this, "In Get Image", Toast.LENGTH_SHORT).show();

builder.setItems(R.array.uploadImage, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent;
switch (which) {
case 0:
intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECT_PHOTO);
break;

case 1:
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTUR_PHOTO);
break;
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK) {
if (resultData != null) {
// this is the image selected by the user
Uri imageUri = resultData.getData();

if (imageUri != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(imageUri);
image = BitmapFactory.decodeStream(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
if (requestCode == CAPTUR_PHOTO && resultCode == RESULT_OK) {
if (resultData != null) {
// this is the image selected by the user
image = (Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data");
}
}
}


}










share|improve this question














When I run an AlertDialog asking the user to get a photo from the camera or the gallery the program doesn't seem to wait for the results and continues to execute. This causes the result to not get saved properly into the Image View field. Ignore the unused variables as I am not done coding this activity yet. I am new to this so any other criticism is appreciated.



package ca.android.whitehead.mycardswallet;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Objects;

public class AddEditCardActivity extends AppCompatActivity implements View.OnClickListener {

private EditText etCardName;
private ImageView ivCardFront, ivCardBack, ivBarcode;
private Button btnCardFront, btnCardBack, btnBarcode;

private Bitmap image;

private static final int SELECT_PHOTO = 1;
private static final int CAPTUR_PHOTO = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit_card);

etCardName = findViewById(R.id.etCardName);
ivCardFront = findViewById(R.id.ivCardFront);
ivCardBack = findViewById(R.id.ivCardBack);
ivBarcode = findViewById(R.id.ivBarcode);
btnCardFront = findViewById(R.id.btnCardFront);
btnCardBack = findViewById(R.id.btnCardBack);
btnBarcode = findViewById(R.id.btnBarcode);

btnCardFront.setOnClickListener(this);
btnCardBack.setOnClickListener(this);
}

@Override
public void onClick(View v)
{
switch (v.getId()){
case R.id.btnCardFront:
getImage();
if (image != null)
{
ivCardFront.setImageBitmap(image);
}
break;
case R.id.btnCardBack:
getImage();
if (image != null)
{
ivCardBack.setImageBitmap(image);
}
break;

}
}

public void getImage()
{
AlertDialog.Builder builder = new AlertDialog.Builder(AddEditCardActivity.this);
builder.setTitle("Pick from gallery or take new picture");
Toast.makeText(this, "In Get Image", Toast.LENGTH_SHORT).show();

builder.setItems(R.array.uploadImage, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent;
switch (which) {
case 0:
intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECT_PHOTO);
break;

case 1:
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTUR_PHOTO);
break;
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK) {
if (resultData != null) {
// this is the image selected by the user
Uri imageUri = resultData.getData();

if (imageUri != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(imageUri);
image = BitmapFactory.decodeStream(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
if (requestCode == CAPTUR_PHOTO && resultCode == RESULT_OK) {
if (resultData != null) {
// this is the image selected by the user
image = (Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data");
}
}
}


}







android alertdialog






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 22:01









Cameron WhiteheadCameron Whitehead

11




11













  • Possible duplicate of Android: wait on user input from dialog?

    – 0X0nosugar
    Jan 2 at 22:10











  • So basically you should put the if (image != null){...} as next statement after image = ... .

    – 0X0nosugar
    Jan 2 at 22:14











  • Both show() on an AlertDialog and startActivityForResult() are asynchronous. Make use of the image if and when you get one, in onActivityResult().

    – CommonsWare
    Jan 2 at 22:15



















  • Possible duplicate of Android: wait on user input from dialog?

    – 0X0nosugar
    Jan 2 at 22:10











  • So basically you should put the if (image != null){...} as next statement after image = ... .

    – 0X0nosugar
    Jan 2 at 22:14











  • Both show() on an AlertDialog and startActivityForResult() are asynchronous. Make use of the image if and when you get one, in onActivityResult().

    – CommonsWare
    Jan 2 at 22:15

















Possible duplicate of Android: wait on user input from dialog?

– 0X0nosugar
Jan 2 at 22:10





Possible duplicate of Android: wait on user input from dialog?

– 0X0nosugar
Jan 2 at 22:10













So basically you should put the if (image != null){...} as next statement after image = ... .

– 0X0nosugar
Jan 2 at 22:14





So basically you should put the if (image != null){...} as next statement after image = ... .

– 0X0nosugar
Jan 2 at 22:14













Both show() on an AlertDialog and startActivityForResult() are asynchronous. Make use of the image if and when you get one, in onActivityResult().

– CommonsWare
Jan 2 at 22:15





Both show() on an AlertDialog and startActivityForResult() are asynchronous. Make use of the image if and when you get one, in onActivityResult().

– CommonsWare
Jan 2 at 22:15












1 Answer
1






active

oldest

votes


















0














I ended up taking CommonsWare advice.



import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Objects;

public class AddEditCardActivity extends AppCompatActivity implements View.OnClickListener {

private EditText etCardName;
private ImageView ivCardFront, ivCardBack, ivBarcode;
private Button btnCardFront, btnCardBack, btnBarcode;

private Bitmap image;

private static final int SELECT_PHOTO = 100;
private static final int CAPTURE_PHOTO = 200;
private static final int FRONT_IMAGE = 1;
private static final int BACK_IMAGE = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit_card);

etCardName = findViewById(R.id.etCardName);
ivCardFront = findViewById(R.id.ivCardFront);
ivCardBack = findViewById(R.id.ivCardBack);
ivBarcode = findViewById(R.id.ivBarcode);
btnCardFront = findViewById(R.id.btnCardFront);
btnCardBack = findViewById(R.id.btnCardBack);
btnBarcode = findViewById(R.id.btnBarcode);

btnCardFront.setOnClickListener(this);
btnCardBack.setOnClickListener(this);
}

@Override
public void onClick(View v)
{
switch (v.getId()){
case R.id.btnCardFront:
getImage(1);
break;
case R.id.btnCardBack:
getImage(2);
break;

}
}

public void getImage(final int image)
{
AlertDialog.Builder builder = new AlertDialog.Builder(AddEditCardActivity.this);
builder.setTitle("Pick from gallery or take new picture");
Toast.makeText(this, "In Get Image", Toast.LENGTH_SHORT).show();

builder.setItems(R.array.uploadImage, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent;
switch (which) {
case 0:
intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECT_PHOTO + image);

break;

case 1:
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_PHOTO + image);
break;
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}

private void setBitmap(Uri imageUri, ImageView imageView)
{
if (imageUri != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(imageUri);
imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if(resultCode == RESULT_OK) {
if (requestCode == (SELECT_PHOTO + FRONT_IMAGE)) {
if (resultData != null) {
setBitmap(resultData.getData(), ivCardFront);
}
}
else if (requestCode == (SELECT_PHOTO + BACK_IMAGE)) {
if (resultData != null) {
setBitmap(resultData.getData(), ivCardBack);
}
}
else if (requestCode == CAPTURE_PHOTO + FRONT_IMAGE) {
if (resultData != null) {
// this is the image selected by the user
ivCardFront.setImageBitmap((Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data"));
}
}
else if (requestCode == CAPTURE_PHOTO + BACK_IMAGE) {
if (resultData != null) {
// this is the image selected by the user
ivCardBack.setImageBitmap((Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data"));
}
}
}
}


}






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013748%2fhow-to-pause-code-for-results-from-alertdialog%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I ended up taking CommonsWare advice.



    import android.content.DialogInterface;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.provider.MediaStore;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Toast;

    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.util.Objects;

    public class AddEditCardActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText etCardName;
    private ImageView ivCardFront, ivCardBack, ivBarcode;
    private Button btnCardFront, btnCardBack, btnBarcode;

    private Bitmap image;

    private static final int SELECT_PHOTO = 100;
    private static final int CAPTURE_PHOTO = 200;
    private static final int FRONT_IMAGE = 1;
    private static final int BACK_IMAGE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_edit_card);

    etCardName = findViewById(R.id.etCardName);
    ivCardFront = findViewById(R.id.ivCardFront);
    ivCardBack = findViewById(R.id.ivCardBack);
    ivBarcode = findViewById(R.id.ivBarcode);
    btnCardFront = findViewById(R.id.btnCardFront);
    btnCardBack = findViewById(R.id.btnCardBack);
    btnBarcode = findViewById(R.id.btnBarcode);

    btnCardFront.setOnClickListener(this);
    btnCardBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
    switch (v.getId()){
    case R.id.btnCardFront:
    getImage(1);
    break;
    case R.id.btnCardBack:
    getImage(2);
    break;

    }
    }

    public void getImage(final int image)
    {
    AlertDialog.Builder builder = new AlertDialog.Builder(AddEditCardActivity.this);
    builder.setTitle("Pick from gallery or take new picture");
    Toast.makeText(this, "In Get Image", Toast.LENGTH_SHORT).show();

    builder.setItems(R.array.uploadImage, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Intent intent;
    switch (which) {
    case 0:
    intent = new Intent(Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, SELECT_PHOTO + image);

    break;

    case 1:
    intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, CAPTURE_PHOTO + image);
    break;
    }
    }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
    }

    private void setBitmap(Uri imageUri, ImageView imageView)
    {
    if (imageUri != null) {
    try {
    InputStream inputStream = getContentResolver().openInputStream(imageUri);
    imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if(resultCode == RESULT_OK) {
    if (requestCode == (SELECT_PHOTO + FRONT_IMAGE)) {
    if (resultData != null) {
    setBitmap(resultData.getData(), ivCardFront);
    }
    }
    else if (requestCode == (SELECT_PHOTO + BACK_IMAGE)) {
    if (resultData != null) {
    setBitmap(resultData.getData(), ivCardBack);
    }
    }
    else if (requestCode == CAPTURE_PHOTO + FRONT_IMAGE) {
    if (resultData != null) {
    // this is the image selected by the user
    ivCardFront.setImageBitmap((Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data"));
    }
    }
    else if (requestCode == CAPTURE_PHOTO + BACK_IMAGE) {
    if (resultData != null) {
    // this is the image selected by the user
    ivCardBack.setImageBitmap((Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data"));
    }
    }
    }
    }


    }






    share|improve this answer




























      0














      I ended up taking CommonsWare advice.



      import android.content.DialogInterface;
      import android.content.Intent;
      import android.graphics.Bitmap;
      import android.graphics.BitmapFactory;
      import android.net.Uri;
      import android.provider.MediaStore;
      import android.support.v7.app.AlertDialog;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.ImageView;
      import android.widget.Toast;

      import java.io.FileNotFoundException;
      import java.io.InputStream;
      import java.util.Objects;

      public class AddEditCardActivity extends AppCompatActivity implements View.OnClickListener {

      private EditText etCardName;
      private ImageView ivCardFront, ivCardBack, ivBarcode;
      private Button btnCardFront, btnCardBack, btnBarcode;

      private Bitmap image;

      private static final int SELECT_PHOTO = 100;
      private static final int CAPTURE_PHOTO = 200;
      private static final int FRONT_IMAGE = 1;
      private static final int BACK_IMAGE = 2;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_add_edit_card);

      etCardName = findViewById(R.id.etCardName);
      ivCardFront = findViewById(R.id.ivCardFront);
      ivCardBack = findViewById(R.id.ivCardBack);
      ivBarcode = findViewById(R.id.ivBarcode);
      btnCardFront = findViewById(R.id.btnCardFront);
      btnCardBack = findViewById(R.id.btnCardBack);
      btnBarcode = findViewById(R.id.btnBarcode);

      btnCardFront.setOnClickListener(this);
      btnCardBack.setOnClickListener(this);
      }

      @Override
      public void onClick(View v)
      {
      switch (v.getId()){
      case R.id.btnCardFront:
      getImage(1);
      break;
      case R.id.btnCardBack:
      getImage(2);
      break;

      }
      }

      public void getImage(final int image)
      {
      AlertDialog.Builder builder = new AlertDialog.Builder(AddEditCardActivity.this);
      builder.setTitle("Pick from gallery or take new picture");
      Toast.makeText(this, "In Get Image", Toast.LENGTH_SHORT).show();

      builder.setItems(R.array.uploadImage, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
      Intent intent;
      switch (which) {
      case 0:
      intent = new Intent(Intent.ACTION_PICK,
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      startActivityForResult(intent, SELECT_PHOTO + image);

      break;

      case 1:
      intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      startActivityForResult(intent, CAPTURE_PHOTO + image);
      break;
      }
      }
      });
      AlertDialog dialog = builder.create();
      dialog.show();
      }

      private void setBitmap(Uri imageUri, ImageView imageView)
      {
      if (imageUri != null) {
      try {
      InputStream inputStream = getContentResolver().openInputStream(imageUri);
      imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream));
      } catch (FileNotFoundException e) {
      e.printStackTrace();
      }
      }
      }

      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
      if(resultCode == RESULT_OK) {
      if (requestCode == (SELECT_PHOTO + FRONT_IMAGE)) {
      if (resultData != null) {
      setBitmap(resultData.getData(), ivCardFront);
      }
      }
      else if (requestCode == (SELECT_PHOTO + BACK_IMAGE)) {
      if (resultData != null) {
      setBitmap(resultData.getData(), ivCardBack);
      }
      }
      else if (requestCode == CAPTURE_PHOTO + FRONT_IMAGE) {
      if (resultData != null) {
      // this is the image selected by the user
      ivCardFront.setImageBitmap((Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data"));
      }
      }
      else if (requestCode == CAPTURE_PHOTO + BACK_IMAGE) {
      if (resultData != null) {
      // this is the image selected by the user
      ivCardBack.setImageBitmap((Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data"));
      }
      }
      }
      }


      }






      share|improve this answer


























        0












        0








        0







        I ended up taking CommonsWare advice.



        import android.content.DialogInterface;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.graphics.BitmapFactory;
        import android.net.Uri;
        import android.provider.MediaStore;
        import android.support.v7.app.AlertDialog;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.ImageView;
        import android.widget.Toast;

        import java.io.FileNotFoundException;
        import java.io.InputStream;
        import java.util.Objects;

        public class AddEditCardActivity extends AppCompatActivity implements View.OnClickListener {

        private EditText etCardName;
        private ImageView ivCardFront, ivCardBack, ivBarcode;
        private Button btnCardFront, btnCardBack, btnBarcode;

        private Bitmap image;

        private static final int SELECT_PHOTO = 100;
        private static final int CAPTURE_PHOTO = 200;
        private static final int FRONT_IMAGE = 1;
        private static final int BACK_IMAGE = 2;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_edit_card);

        etCardName = findViewById(R.id.etCardName);
        ivCardFront = findViewById(R.id.ivCardFront);
        ivCardBack = findViewById(R.id.ivCardBack);
        ivBarcode = findViewById(R.id.ivBarcode);
        btnCardFront = findViewById(R.id.btnCardFront);
        btnCardBack = findViewById(R.id.btnCardBack);
        btnBarcode = findViewById(R.id.btnBarcode);

        btnCardFront.setOnClickListener(this);
        btnCardBack.setOnClickListener(this);
        }

        @Override
        public void onClick(View v)
        {
        switch (v.getId()){
        case R.id.btnCardFront:
        getImage(1);
        break;
        case R.id.btnCardBack:
        getImage(2);
        break;

        }
        }

        public void getImage(final int image)
        {
        AlertDialog.Builder builder = new AlertDialog.Builder(AddEditCardActivity.this);
        builder.setTitle("Pick from gallery or take new picture");
        Toast.makeText(this, "In Get Image", Toast.LENGTH_SHORT).show();

        builder.setItems(R.array.uploadImage, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        Intent intent;
        switch (which) {
        case 0:
        intent = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, SELECT_PHOTO + image);

        break;

        case 1:
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAPTURE_PHOTO + image);
        break;
        }
        }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
        }

        private void setBitmap(Uri imageUri, ImageView imageView)
        {
        if (imageUri != null) {
        try {
        InputStream inputStream = getContentResolver().openInputStream(imageUri);
        imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream));
        } catch (FileNotFoundException e) {
        e.printStackTrace();
        }
        }
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
        if(resultCode == RESULT_OK) {
        if (requestCode == (SELECT_PHOTO + FRONT_IMAGE)) {
        if (resultData != null) {
        setBitmap(resultData.getData(), ivCardFront);
        }
        }
        else if (requestCode == (SELECT_PHOTO + BACK_IMAGE)) {
        if (resultData != null) {
        setBitmap(resultData.getData(), ivCardBack);
        }
        }
        else if (requestCode == CAPTURE_PHOTO + FRONT_IMAGE) {
        if (resultData != null) {
        // this is the image selected by the user
        ivCardFront.setImageBitmap((Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data"));
        }
        }
        else if (requestCode == CAPTURE_PHOTO + BACK_IMAGE) {
        if (resultData != null) {
        // this is the image selected by the user
        ivCardBack.setImageBitmap((Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data"));
        }
        }
        }
        }


        }






        share|improve this answer













        I ended up taking CommonsWare advice.



        import android.content.DialogInterface;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.graphics.BitmapFactory;
        import android.net.Uri;
        import android.provider.MediaStore;
        import android.support.v7.app.AlertDialog;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.ImageView;
        import android.widget.Toast;

        import java.io.FileNotFoundException;
        import java.io.InputStream;
        import java.util.Objects;

        public class AddEditCardActivity extends AppCompatActivity implements View.OnClickListener {

        private EditText etCardName;
        private ImageView ivCardFront, ivCardBack, ivBarcode;
        private Button btnCardFront, btnCardBack, btnBarcode;

        private Bitmap image;

        private static final int SELECT_PHOTO = 100;
        private static final int CAPTURE_PHOTO = 200;
        private static final int FRONT_IMAGE = 1;
        private static final int BACK_IMAGE = 2;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_edit_card);

        etCardName = findViewById(R.id.etCardName);
        ivCardFront = findViewById(R.id.ivCardFront);
        ivCardBack = findViewById(R.id.ivCardBack);
        ivBarcode = findViewById(R.id.ivBarcode);
        btnCardFront = findViewById(R.id.btnCardFront);
        btnCardBack = findViewById(R.id.btnCardBack);
        btnBarcode = findViewById(R.id.btnBarcode);

        btnCardFront.setOnClickListener(this);
        btnCardBack.setOnClickListener(this);
        }

        @Override
        public void onClick(View v)
        {
        switch (v.getId()){
        case R.id.btnCardFront:
        getImage(1);
        break;
        case R.id.btnCardBack:
        getImage(2);
        break;

        }
        }

        public void getImage(final int image)
        {
        AlertDialog.Builder builder = new AlertDialog.Builder(AddEditCardActivity.this);
        builder.setTitle("Pick from gallery or take new picture");
        Toast.makeText(this, "In Get Image", Toast.LENGTH_SHORT).show();

        builder.setItems(R.array.uploadImage, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        Intent intent;
        switch (which) {
        case 0:
        intent = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, SELECT_PHOTO + image);

        break;

        case 1:
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAPTURE_PHOTO + image);
        break;
        }
        }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
        }

        private void setBitmap(Uri imageUri, ImageView imageView)
        {
        if (imageUri != null) {
        try {
        InputStream inputStream = getContentResolver().openInputStream(imageUri);
        imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream));
        } catch (FileNotFoundException e) {
        e.printStackTrace();
        }
        }
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
        if(resultCode == RESULT_OK) {
        if (requestCode == (SELECT_PHOTO + FRONT_IMAGE)) {
        if (resultData != null) {
        setBitmap(resultData.getData(), ivCardFront);
        }
        }
        else if (requestCode == (SELECT_PHOTO + BACK_IMAGE)) {
        if (resultData != null) {
        setBitmap(resultData.getData(), ivCardBack);
        }
        }
        else if (requestCode == CAPTURE_PHOTO + FRONT_IMAGE) {
        if (resultData != null) {
        // this is the image selected by the user
        ivCardFront.setImageBitmap((Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data"));
        }
        }
        else if (requestCode == CAPTURE_PHOTO + BACK_IMAGE) {
        if (resultData != null) {
        // this is the image selected by the user
        ivCardBack.setImageBitmap((Bitmap)Objects.requireNonNull(resultData.getExtras()).get("data"));
        }
        }
        }
        }


        }







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 0:20









        Cameron WhiteheadCameron Whitehead

        11




        11
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013748%2fhow-to-pause-code-for-results-from-alertdialog%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]