如何为AlertDialog中的结果暂停代码

时间:2019-01-02 22:01:12

标签: android alertdialog

当我运行AlertDialog要求用户从相机或图库中获取照片时,该程序似乎没有等待结果并继续执行。这会导致结果无法正确保存到“图像视图”字段中。忽略未使用的变量,因为我尚未完成此活动的编码。我对此并不陌生,因此请接受其他批评。

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

}

1 个答案:

答案 0 :(得分:0)

我最终接受了CommonsWare的建议。

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

}