2 つの ImageView と 2 つのボタンがあります。ボタンが押されたとき - ダイアログが開始されました。肯定的な答え - ギャラリーからの写真、否定的な答え - カメラから。まず、アクティビティにこれを書きます(このコードはうまくいきます):
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
String path = null;
if (DialogFlag == 0) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case GALLERY_REQUEST:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
path = getRealPathFromURI(selectedImage);
Log.d("myLogs", path);
if (btnID == 1) {
pathOne = path;
Bitmap bmImg = BitmapFactory.decodeFile(pathOne);
ivOne.setImageBitmap(bmImg);
one = bmImg;
} else {
pathTwo = path;
Bitmap bmImg = BitmapFactory.decodeFile(pathTwo);
ivTwo.setImageBitmap(bmImg);
two = bmImg;
}
}
}
}
if (DialogFlag == 1) {
Uri uri;
if (requestCode == CAMERA_RESULT) {
Cursor cursor = getContentResolver().query(
Media.EXTERNAL_CONTENT_URI,
new String[] { Media.DATA, Media.DATE_ADDED,
MediaStore.Images.ImageColumns.ORIENTATION },
Media.DATE_ADDED, null, "date_added ASC");
if (cursor != null && cursor.moveToFirst()) {
do {
uri = Uri.parse(cursor.getString(cursor
.getColumnIndex(Media.DATA)));
path = uri.toString();
} while (cursor.moveToNext());
cursor.close();
}
Log.d("myLogs", path);
if (btnID == 1) {
pathOne = path;
Bitmap bmImg = BitmapFactory.decodeFile(pathOne);
ivOne.setImageBitmap(bmImg);
one = bmImg;
} else {
pathTwo = path;
Bitmap bmImg = BitmapFactory.decodeFile(pathTwo);
ivTwo.setImageBitmap(bmImg);
two = bmImg;
}
}
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case IDD_TWO_BUTTONS:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Зображення з ")
.setCancelable(false)
.setPositiveButton("Галереї",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
DialogFlag = 0;
Intent photoPickerIntent = new Intent(
Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,
GALLERY_REQUEST);
dialog.cancel();
}
})
.setNeutralButton("Камери",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
DialogFlag = 1;
Intent cameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,
CAMERA_RESULT);
dialog.cancel();
}
}
)
.setNegativeButton("Відміна",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
}
);
return builder.create();
default:
return null;
}
}
しかし、これをフラグメントで作成しようとすると、問題が発生します。
public class MyDialogFragment extends DialogFragment {
public static MyDialogFragment newInstance(int title) {
MyDialogFragment frag = new MyDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.plus_icon)
.setTitle(title).setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent i =getActivity().getIntent();
i.putExtra("key", true);
getTargetFragment().onActivityResult(getTargetRequestCode(), 101, i);
}
}
)
.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//AddExerciseFragment.doNegativeClick();
Intent i =getActivity().getIntent();
i.putExtra("key", false);
getTargetFragment().onActivityResult(getTargetRequestCode(), 101, i);
}
}
)
.create();
}
}
AddExerciseFragment の onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
String path = null;
switch (requestCode) {
case DIALOG_FRAGMENT:
boolean check = imageReturnedIntent.getBooleanExtra("key", true);
if (check) {
doPositiveClick();
if (resultCode == RESULT_OK && requestCode == GALLERY_REQUEST) {
Uri selectedImage = imageReturnedIntent.getData();
path = getRealPathFromURI(selectedImage);
Log.d("myLogs", path);
if (btnID == 1) {
pathOne = path;
Bitmap bmImg = BitmapFactory.decodeFile(pathOne);
ivOne.setImageBitmap(bmImg);
one = bmImg;
} else {
pathTwo = path;
Bitmap bmImg = BitmapFactory.decodeFile(pathTwo);
ivTwo.setImageBitmap(bmImg);
two = bmImg;
}
}
} else {
doNegativeClick();
Uri uri;
Cursor cursor = getActivity().getContentResolver().query(
Media.EXTERNAL_CONTENT_URI,
new String[] { Media.DATA, Media.DATE_ADDED,
MediaStore.Images.ImageColumns.ORIENTATION },
Media.DATE_ADDED, null, "date_added ASC");
if (cursor != null && cursor.moveToFirst()) {
do {
uri = Uri.parse(cursor.getString(cursor
.getColumnIndex(Media.DATA)));
path = uri.toString();
} while (cursor.moveToNext());
cursor.close();
}
Log.d("myLogs", path);
if (btnID == 1) {
pathOne = path;
Bitmap bmImg = BitmapFactory.decodeFile(pathOne);
ivOne.setImageBitmap(bmImg);
one = bmImg;
} else {
pathTwo = path;
Bitmap bmImg = BitmapFactory.decodeFile(pathTwo);
ivTwo.setImageBitmap(bmImg);
two = bmImg;
}
}
break;
}
}
public void doPositiveClick() {
DialogFlag = 0;
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
// dialog.cancel();
}
public void doNegativeClick() {
DialogFlag = 1;
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_RESULT);
}
void showDialog() {
DialogFragment newFragment = MyDialogFragment
.newInstance(R.string.name);
newFragment.show(getFragmentManager(), "dialog");
}
しかし、私は次のものを持っています:
- ギャラリーから画像を設定したい場合-ギャラリーを起動しますが、そこで画像を選択できません
- カメラから、1回だけ、1つのimageViewに対してのみ画像を設定できます
- ギャラリーで「戻る」ボタンを押すと、エラーが発生します。