Choose image from gallery or take from camera
Solution 1
Add a alertDialog
and then in onActivityResult
instead of case of 0 and 1 use the REQUEST_CAMERA
and SELECT_FILE
. which you'll declare as in code:
private static final int REQUEST_CAMERA = 1;
private static final int SELECT_FILE = 2;
final CharSequence[] items = {"Take Photo", "Choose from Library", "Cancel"};
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mActivity);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
PROFILE_PIC_COUNT = 1;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
PROFILE_PIC_COUNT = 1;
Intent intent = new Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,SELECT_FILE);
} else if (items[item].equals("Cancel")) {
PROFILE_PIC_COUNT = 0;
dialog.dismiss();
}
}
});
builder.show();
Solution 2
You should first decide like what action you want to perform like as follows
public class PicModeSelectDialogFragment extends DialogFragment {
private String[] picMode = {"Camera", "Gallery"};
private IPicModeSelectListener iPicModeSelectListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setItems(picMode, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (iPicModeSelectListener != null)
iPicModeSelectListener.onPicModeSelected(Constants.PicModes.values()[which]);
}
});
return builder.create();
}
public void setiPicModeSelectListener(IPicModeSelectListener iPicModeSelectListener) {
this.iPicModeSelectListener = iPicModeSelectListener;
}
public interface IPicModeSelectListener {
void onPicModeSelected(Constants.PicModes modes);
}
}
After that you can fire intent to open camera or gallery like follows:
if(openCamera){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
}else{
//open gallery
Intent intent = new Intent(Intent.ACTION_GET_CONTENT).setType("image/*");
}
Solution 3
First of all create a dialog with two buttons that will ask user to choose either Gallery
or Camera.
On Click of buttons set Intent for picking the images like below:
public void dialog()
{
final Dialog dialog = new Dialog(getActivity(), R.style.cust_dialog);
dialog.setTitle("Upload From");
dialog.setContentView(R.layout.dialog_pop_up_gallery_camera);
dialog.setTitle("Select an Option...");
TextView txt_gallry=(TextView)dialog.findViewById(R.id.textView_gallery);
TextView txt_camera=(TextView)dialog.findViewById(R.id.textView_camera);
txt_gallry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
startActivityForResult(i, PHOTO_PICKER_ID);
}
});
txt_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
File fil = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fil));
startActivityForResult(cameraIntent, REQUEST_CODE_CAPTURE_IMAGE);
}
});
dialog.show();
}
Here is the layout file for dialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|center_vertical"
android:background="@color/lightpurple">
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Gallery"
android:textColor="@color/white"
android:textSize="18sp"
android:padding="10dp"
android:drawableLeft="@drawable/galery_small"
android:drawablePadding="15dp"
android:id="@+id/textView_gallery"/>
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="18sp"
android:text="Camera"
android:drawablePadding="15dp"
android:drawableLeft="@drawable/camera"
android:padding="10dp"
android:id="@+id/textView_camera"/>
</LinearLayout>
Burhanuddin Rabbani
Updated on June 05, 2022Comments
-
Burhanuddin Rabbani almost 2 years
I have to make a button that will provide to choose image from gallery or take from camera.
private void showFileChooser() { Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePicture, 0); Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(pickPhoto , 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 0: if(resultCode == RESULT_OK){ Uri selectedImage = imageReturnedIntent.getData(); imageView.setImageURI(selectedImage); } break; case 1: if(resultCode == RESULT_OK){ Uri selectedImage = imageReturnedIntent.getData(); imageView.setImageURI(selectedImage); } break; } }
The result is working. If i choose from gallery, the imageviewer will view that, it also working if i choose taking photo from camera. The problem is, in my showFileChooser() method, all of my intent are runing in same time, so when i choos from gallery, the camera still running also. I choose camera, the gallery is opening too. I think i should implement my code in switch case mode but i dont understand how to do that. Please kindly help to solve my beginner problem.
-
Burhanuddin Rabbani about 8 yearsis this a class? but I implement my code in a method called showFileChooser()
-
Burhanuddin Rabbani about 8 yearshow to implement this inside my method called showFileChooser()? I dont use direct implementation, but i used a button to call that method, then in my post is the method declaration
-
Burhanuddin Rabbani about 8 yearsso, i should make another activity outside my mainactivity?
-
Android Geek about 8 yearsNo, Just put this code in you main activity and call this dialog on button click, where you were calling
showFileChooser()
and also put thatxml
in yourres/layout
folder. -
D Agrawal about 8 yearsJust add this whole code in your showFileChooser() method . youll be good to go. This basically adds a alertdialog for user to choose between gallery and camera and then call startActivityForResult() accordingly.
-
D Agrawal about 8 yearsDeclatre these two lines globally though private static final int REQUEST_CAMERA = 1; private static final int SELECT_FILE = 2;
-
Android Geek about 8 years@BurhanuddinRabbani are you done with that? Or need help?
-
Ashlesha Sharma about 8 yearsThen you can also first open a dialog in showFileChooser() asking user for his action and then onItemClick() you can perform action accordingly.
-
aya salama about 7 yearswould you illustrate your pattern plz