如何在对话框中从图库中拍摄照片? - Android

时间:2015-01-01 23:26:07

标签: android android-dialog android-gallery

我试图在对话框上从画廊拍照,但我无法做到。我不能调用startActivityResult()方法来覆盖Dialog。有人知道在Dialog上做到这一点的方法吗?

这是我的代码。 (我需要在buttonG.setOnClickListener())

中完成
public class AddBirthdayDialog extends Dialog {

private Context context;
String name;
String sname;
private int day;
private int month;
private int year;
private byte[] imageByte;
private Bitmap imageBitmap;
private int RESULT_LOAD_IMAGE = 1;

final EditText textName;
final EditText textSname;
final DatePicker birthDate;

private DB myDB;
private SQLiteDatabase database;
private Person person = new Person();

public AddBirthdayDialog(final Context context) {
    super(context);
    this.context = context;

    setCanceledOnTouchOutside(false);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.addbirthday_layout);

    myDB = new DB(context);

    textName = (EditText) findViewById(R.id.editTextname);
    textSname = (EditText) findViewById(R.id.editTextsname);
    birthDate = (DatePicker) findViewById(R.id.datePicker);

    Button buttonG = (Button) findViewById(R.id.buttonUploadG);
    buttonG.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             **//Need to call gallery and take a photo's bitmap or byte[] here.**
        }
    });


    Button buttonC = (Button) findViewById(R.id.buttonUploadC);
    buttonC.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });



    Button cancelButton = (Button) findViewById(R.id.cancelbutton);
    cancelButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            dismiss();

        }
    });

    Button save = (Button) findViewById(R.id.buttonadd);
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            name = textName.getText().toString();
            sname = textSname.getText().toString();

            day = birthDate.getDayOfMonth();
            month = birthDate.getMonth()+1;
            year = birthDate.getYear();
            final String dayStr = String.valueOf(day);
            final String monthStr = String.valueOf(month+1);
            final String yearStr = String.valueOf(year);

            person.setName(name);
            person.setSname(sname);
            person.setDay(day);
            person.setMonth(month);
            person.setYear(year);
            person.setDateStr(dayStr + "/" + monthStr + "/" + yearStr);

            myDB.addBirthday(person);
            if(context instanceof MainActivity)
            ((MainActivity)context).onResume();
            dismiss();

        }
    });
}
}

提前致谢:)

2 个答案:

答案 0 :(得分:1)

你班上的

@SuppressLint("NewApi")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case SELECTED_PICTURE:
        if (resultCode == RESULT_OK) {
            Uri uri = data.getData();
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(uri, projection,
                    null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(projection[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            selectedImage = BitmapFactory.decodeFile(filePath);
            d = new BitmapDrawable(selectedImage);
            iv.setBackground(d);
            update.setVisibility(View.VISIBLE);
        }
        break;
    }
}

在onClick内部:

Intent i = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, SELECTED_PICTURE);

全局变量:

private static final int SELECTED_PICTURE = 1;
Bitmap selectedImage;

答案 1 :(得分:0)

什么是背景?如果它是一个活动,您可以从上下文活动开始活动并在上下文活动中处理onActivityResult