You may need to create a custom dialog for this:
Here is the code for the method that should be called when the user clicks on a specific button:
private void ChooseGallerOrCamera() { final CharSequence[] items = { "Take Photo", "Choose from Gallery", "Cancel" }; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Add Photo!"); builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { if (items[item].equals("Take Photo")) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File f = new File(android.os.Environment .getExternalStorageDirectory(), "MyImage.jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); startActivityForResult(intent, REQUEST_CAMERA); } else if (items[item].equals("Choose from Gallery")) { Intent intent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); startActivityForResult( Intent.createChooser(intent, "Select File"), SELECT_FILE); } else if (items[item].equals("Cancel")) { dialog.dismiss(); } } }); builder.show(); }
And the code to handle onActivityResult() :
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { Bitmap mBitmap; if (requestCode == REQUEST_CAMERA) { File camFile = new File(Environment.getExternalStorageDirectory() .toString()); for (File temp : camFile.listFiles()) { if (temp.getName().equals("MyImage.jpg")) { camFile = temp; break; } } try { BitmapFactory.Options btmapOptions = new BitmapFactory.Options(); mBitmap = BitmapFactory.decodeFile(camFile.getAbsolutePath(), btmapOptions); //Here you have the bitmap of the image from camera } catch (Exception e) { e.printStackTrace(); } } else if (requestCode == SELECT_FILE) { Uri selectedImageUri = data.getData(); String tempPath = getPath(selectedImageUri, MyActivity.this); BitmapFactory.Options btmapOptions = new BitmapFactory.Options(); mBitmap = BitmapFactory.decodeFile(tempPath, btmapOptions); //Here you have the bitmap of the image from gallery } } } public String getPath(Uri uri, Activity activity) { String[] projection = { MediaColumns.DATA }; Cursor cursor = activity .managedQuery(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }
EDIT: Try using this:
private void letUserTakeUserTakePicture() { Intent pickIntent = new Intent(); pickIntent.setType("image/*"); pickIntent.setAction(Intent.ACTION_GET_CONTENT); Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(getActivity()))); String pickTitle = "Select or take a new Picture";
Sagar pilkhwal
source share