Dialog like complete action using - android

I want to create a dialog like this:
I need to create the dialog when user clicks on the profile image like given in link. Current I'm doing this
final CharSequence[] items = { "Gallery", "Camera", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Gallery")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp1.jpg");
mImageCaptureUri = Uri.fromFile(f);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(intent, CAMERA_CODE);
} else if (items[item].equals("Camera")) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, GALLERY_CODE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
I dont know how to do as the design given please help me on this.

There are two ways you can achieve this.
Using Custom Alert dialog.
Using Intent by query intent activities.
By default, Android doesn't show the option to open camera and gallery in a single shot. But doing some tricks will help to achieve this.
Refer the below code which is used to display Camera and Gallery browse options in a single default dialog.
// Picks Camera first.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent,
"Select Image from");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, TAKE_PHOTO_CODE);
Already answered here for same type of issue. !!
--> queryIntentActivityOptions not working
Hope this helps you,

Related

Android - Why can't I start the choosePictureIntent?

Im trying to start the choosePictureIntent. This is done by clicking on an image in an Ap
#Override
public void imageClicked(int position) {
if(data.get(position).getImgUri() != null) {
// IGNORE THIS
Intent intent= new Intent(context, FullScreenActivity.class);
intent.putExtra("data", data.get(position));
context.startActivity(intent);
} else {
// THIS IS THE CODE
Object[] chooseData;
chooseData = Utils.getChoosePictureIntent(context, context.getPackageManager());
Intent chooserIntent = (Intent) chooseData[0];
chooserIntent.putExtra("data", data.get(position)); // data is an array of information
chooserIntent.putExtra("outputFileUri", (Uri) chooseData[1]);
((Activity) context).startActivityForResult(chooserIntent, PICTURE_REQUEST);
}
}
When I click on the image, nothing happens, but I know that the else statement gets executed. After that, if I click on the view besides the image, the whole app freezes.
There are no error messages.
Any help is appreciated.
The source of getChoosePictureIntent():
public static String getUniqueFileName(String prefix, String surfix) {
return prefix + System.currentTimeMillis() + surfix;
}
public static Object[] getChoosePictureIntent(Context context, PackageManager manager) {
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "imagedir" + File.separator);
root.mkdirs();
final String fName = Utils.getUniqueFileName("img_purchase_", ".jpg");
final File sdImageMainDirectory = new File(root, fName);
Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = manager;
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, context.getString(R.string.picture_chooser));
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
return new Object[]{chooserIntent, outputFileUri};
}
If you are using targetSdk >=24, then you need to add File Provider in order to replace the File Uri received from Camera App to Content Uri because API level 24 has blocked File Uri from another app for security reason.You can check out this link for more info

How to avoid using MediaStore.EXTRA_OUTPUT on camera intent?

I'm trying to allow my user to pick a picture from the phone gallery or to take a picture with the camera.
I wrote a piece of code working really well on my Samsung S6
private void openPictureIntent() {
final List<Intent> cameraIntents = new ArrayList<>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, SELECT_PICTURE_CODE);
}
Then I tried it on a Nexus 5X and my app crashed. I learned that the intent retrieved in onActivityResult is null with some devices. The solution is to give the uri where to save the image taken with the camera. I implemented it and my code became
private void openPictureIntent() {
// Determine Uri of camera image to save.
final File dir = new File(Environment.getExternalStorageDirectory() + File.separator + "Directory" + File.separator);
dir.mkdirs();
final String fname = UUID.randomUUID() + ".jpg";
final File sdImageMainDirectory = new File(dir, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
new File(outputFileUri.getPath()).delete();
// Camera.
final List<Intent> cameraIntents = new ArrayList<>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, SELECT_PICTURE_CODE);
}
This is working but I'm not satisfied. I would like my photo to be stored with other camera photo and not in a special directory. How could I achieve that ?
I learned that the intent retrieved in onActivityResult is null with some devices.
It is supposed to be null for all camera apps. Some camera app developers fail to read the documentation for ACTION_IMAGE_CAPTURE.
I would like my photo to be stored with other camera photo and not in a special directory
That is not supported by ACTION_IMAGE_CAPTURE.

Android Chooser Intent for Camera Gallery AND Documents

I'm having trouble creating a chooser intent that allows the users to select either a Camera App, a Gallery App, or a File Browser App.
So far, I can only get Camera + Gallery or Camera + File Browser but not all 3 options
In the below method it seems like following line is being ignored:
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, galleryIntent);
Code:
public void openUploadIntent() {
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fname = "ABCD_" + timeStamp;
final File sdImageMainDirectory = new File(storageDir, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
//Gallery.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
//Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Filesystem.
final Intent fsIntent = new Intent();
fsIntent.setType("*/*");
fsIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(fsIntent, "Select Source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, galleryIntent);
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.ize()]));
startActivityForResult(chooserIntent, 99999);
}
Well i figured it out.
It looks like each call to EXTRA_INTIAL_INTENTS replaces the last.
This is my final working code (on Samsung Galaxy S)
public void openImageIntent() {
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fname = "ABCD_" + timeStamp;
final File sdImageMainDirectory = new File(storageDir, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
//Gallery.
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Filesystem.
final Intent fsIntent = new Intent();
fsIntent.setType("*/*");
fsIntent.setAction(Intent.ACTION_GET_CONTENT);
cameraIntents.add(fsIntent);
//Create the Chooser
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, 99999);
}

Android Studio - Set icon and change font colour

In my app I have added code to allow the user to either select an image from the gallery or take a photo using the camera. This happens when the camera icon is selected at the top right corner, and a dialog box appears with both those options. I am just wondering how i would add an icon which would be used as buttons. So for example when the dialog box appears i want an image of the android gallery icon that when selected takes you into the gallery. Also want the text 'Gallery' in a different font colour.
Here is the code used to create the options
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.launch_voip_call) {
Utils.startCall(this, contact);
return true;
} else if (item.getItemId() == R.id.launch_camera) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Pick Image from")
.setPositiveButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//camera intent
Intent cameraIntent = new Intent(ConversationActivity.this, CameraActivity.class);
cameraIntent.putExtra("EXTRA_CONTACT_JID", contact.getJid());
startActivity(cameraIntent);
}
})
.setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
AlertDialog alert = builder.create();
alert.show();
}
return false;
}
The solution is to create an activity which will list all images from your gallery (as it is your own activity, you can have any icon or font). Besides that you cann't change default gallery application
Here is partial code to select the image from gallery or take it with the help of camera...
You can see the full code at https://gist.github.com/Mariovc/f06e70ebe8ca52fbbbe2
public static Intent getPickImageIntent(Context context){
Intent chooserIntent = null;
List<Intent> intentList = new ArrayList<>();
Intent pickIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhotoIntent.putExtra("return-data", true);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
intentList = addIntentsToList(context, intentList, pickIntent);
intentList = addIntentsToList(context, intentList, takePhotoIntent);
if (intentList.size() > 0) {
chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
context.getString(R.string.pick_image_intent_text));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
}
return chooserIntent;
}
private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) {
List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedIntent = new Intent(intent);
targetedIntent.setPackage(packageName);
list.add(targetedIntent);
}
return list;
}
Add the below given lines before AlertDialog alert = builder.create();
dialog.setOnShowListener(new OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Button button = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
button.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.your_gallery_resource,
Drawable drawable = getActivity().getResources().getDrawable(
android.R.drawable.your_gallery_resource);
// set the bounds to place the drawable a bit right
drawable.setBounds((int) (drawable.getIntrinsicWidth() * 0.5),
0, (int) (drawable.getIntrinsicWidth() * 1.5),
drawable.getIntrinsicHeight());
button.setCompoundDrawables(drawable, null, null, null);
// could modify the placement more here if desired
// button.setCompoundDrawablePadding();
}
});
NOTE: The above code is only for gallery button, do it in a similar way for camera button.

Camera intent doesn't show in intent chooser

String chooseTitle = activity.getString(R.string.select_or_take_picture);
Intent getIntent = new Intent();
getIntent.setType("image/*");
getIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
PackageManager pm = activity.getApplicationContext().getPackageManager();
for (ResolveInfo ri: pm.queryIntentActivities(galleryIntent, PackageManager.MATCH_DEFAULT_ONLY)) {
Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
intent.setAction(Intent.ACTION_PICK);
intents.add(intent);
}
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, photoUri);
for (ResolveInfo ri : pm.queryIntentActivities(cameraIntent, 0)) {
Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
intents.add(intent);
}
Intent chooserIntent = Intent.createChooser(getIntent, chooseTitle);
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS,
intents.toArray(new Parcelable[] {})
);
By doing this way, the chooser shows:
The camera intent doesn't show at all.
But if I change the line
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, photoUri);
to
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
It works just fine:
But the problem is, I really want to pass photoUri. How can I do with it?
I know a possible alternative is to write my own chooser dialog, but I do want to know if it's a bug in intent chooser, or if I don't use it correctly.
p.s.
#dkarmazi, Here's how I generate Uri:
public Uri generatePhotoUri() {
String timeStamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
String imageFileName = "XXX_" + timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File imageFile = new File(storageDir, imageFileName);
return Uri.fromFile(imageFile);
}
Here's my onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_PHOTO:
if (resultCode != Activity.RESULT_OK) {
break;
}
Uri source = data == null ?
mPhotoUri : // take picture
data.getData(); // choose from other app
if (source == null) {
break;
}
// TODO: do with source
break;
}
//....
}
#dkarmazi, I've debugged it, and I make sure resultCode is RESULT_CANCELED.
I recently worked on the same problem and here is my solution:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
Then, once the user takes a picture, you should be able to access it by using the provided photoUri
Here is some documentation on EXTRA_OUTPUT and you can also lookup ACTION_IMAGE_CAPTURE on the same page.
UPDATE on Intent Chooser:
// we create intent chooser by picking one of the intents
Intent chooserIntent = Intent.createChooser(cameraIntent, getResources().getString(R.string.pick_action_string_for_user));
// then we add any additional intents
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { getIntent });
// chooserIntent is ready
startActivityForResult(chooserIntent, requestCode);
Thank #dkarmazi very much for helping me debug this issue. It seems we haven't enough reputation to refine the answer together, so I'm posting the solution here. Later in our chat we find a probable cause, and after my experimenting, now it works. It's actually an issue how I gather cameraIntents, so change this part of code
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, photoUri);
for (ResolveInfo ri : pm.queryIntentActivities(cameraIntent, 0)) {
Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
intents.add(intent);
}
to
final Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
for(ResolveInfo ri : pm.queryIntentActivities(cameraIntent, 0)) {
final String packageName = ri.activityInfo.packageName;
final Intent intent = new Intent(cameraIntent);
intent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
intents.add(intent);
}
Also need to change
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
PackageManager pm = activity.getApplicationContext().getPackageManager();
for (ResolveInfo ri: pm.queryIntentActivities(galleryIntent, PackageManager.MATCH_DEFAULT_ONLY)) {
Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
intent.setAction(Intent.ACTION_PICK);
intents.add(intent);
}
to
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("image/*");
PackageManager pm = activity.getApplicationContext().getPackageManager();
for (ResolveInfo ri: pm.queryIntentActivities(galleryIntent, PackageManager.MATCH_DEFAULT_ONLY)) {
final String packageName = ri.activityInfo.packageName;
final Intent intent = new Intent(galleryIntent);
intent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name));
intent.setPackage(packageName);
intents.add(intent);
}
I hate the boilerplate of it, but finally it works.

Categories

Resources