How can I open an image chooser and show an option to take a picture as well?
Here's my code:
private void openFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
The problem is that there's no open camera option. How do I add that?
And this is what I get:
You can use this library or show a normal alert dialog with two buttons, one for gallery and one for camera.
There is a similar question: Dialog to pick image from gallery or from camera
You have to specify an action for invoking a chooser/app via an intent. And since the action for selecting an image from gallery and taking a new image using the camera, are different, i don't think a chooser will have both option for uploading an image and taking a new image at the same time. For reference you could check profile picture changing procedure in facebook or whatsaapp. When clicking on the image to change, a custom dialog is shown with the option to either upload an image, take a new image, or select from a previously uploaded image. Based on what the user selects the app navigates the user to the particular app(camera/gallery etc)
What you can do is first create a custom dialog(or a layout based on your application's design) with two options
Take a new image.
Upload an Image from gallery.
Based on what the user clicks execute the code to navigate the user to, either the gallery or the camera application. If the user clicks on Upload an image from gallery, execute your above code and if the user clicks on Take a new image, then execute the code for opening the camera.
private void takeNewImage() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent,CAMERA_INTENT_REQ_CODE);
}
Related
I am using Android Studio for a simple communication app and this definitly might be a stupid question but I couldn't find the answer to it yet.
I am simply starting a new intent on a button click. However, the very first time the user does this, he is asked what kind of app he wants to use. Therefore, there are at least two applications with the same intent filter, namely "ACTION_VIEW"
My Problem:
After the initial click the button always "reuses" his initial choice so the user does not have a decision anymore which app he wants to use for this intent.
Unfortunately, this is excactly what I want. The user should get the chance to select the app of his favor each time he clicks on the button.
The relevant part of code is as simple as follows:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
Intent.FLAG_UPDATE_CURRENT could probably help you.
https://developer.android.com/reference/android/app/PendingIntent.html
You can use an App Chooser to show the dialog for the user to choose which app to use each time:
[I]f the action to be performed could be handled by multiple apps and the user might prefer a different app each time—such as a "share" action, for which users might have several apps through which they might share an item—you should explicitly show a chooser dialog.... The chooser dialog forces the user to select which app to use for the action every time (the user cannot select a default app for the action).
Intent intent = new Intent(Intent.ACTION_SEND);
...
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
This is the code that opens the gallery when my button is pressed.
public void openGallery(View v){
if (v.getId() == R.id.GALLERY){
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);
}
}
This code works to open the gallery, but when the user presses the pictures they want, is there a way to place those pictures into a queue int the order the user presses them in?
is there a way to place those pictures into a queue int the order the user presses them in?
No. If you want that, do not use ACTION_GET_CONTENT, but instead create your own gallery-style UI where you have complete control over the user interaction.
Or, do not worry about the order from ACTION_GET_CONTENT, but instead have the user put the images in the proper order in your own UI.
I want the gallery app to be launched in a separate window, not in my app. I also dont want to choose a picture, I just want to open the default gallery app.
Some questions are very similiar to this one, but they all open the gallery not as a standalone app, always inside of the app which has called
startActivity(intent);.
see here. This is my app called SM2. inside, the default gallery app is visible, which is not the desired behaviour.
The following code has no use if there is no package named 'com.android.gallery' on the phone:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.gallery");
if (launchIntent != null) {
startActivity(launchIntent);//null pointer check in case package name was not found
}
this opens a gallery in my app, not as wished as a standalone task:
Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES)));
startActivity(intent1);
and this does also open the gallery in my app:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media/"));
startActivity(intent);
It is launched inside of the application
It is being launched in your task. You can use FLAG_ACTIVITY_NEW_TASK on your Intent to have it launch in its own task.
I also dont want to choose a picture, I just want to open the default gallery app
You are welcome to try using CATEGORY_APP_GALLERY, though not all gallery apps might have an activity that supports this.
Is there a built-in way for a grid bottom sheet, usually seen for sharing in some apps, or do I have to create a custom BottomSheet?
//1 column bottom sheet
String message = "Text I want to share.";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(share, "Share through:"));
The intent chooser is a property of the OS and you can't really change it. As you might have noticed, this chooser has changed its appearance in the different releases of the OS. The apps in which you have seen a grid chooser might have implemented a custom bottom sheet or activity to look like a bottom sheet. You can achieve this by using the Android PackageManager. You can obtain the different apps that can handle your intent and show these in your custom chooser and then send the intent to the app which the user clicks on.
I am building an application for Android 2.2 which is based on a photo library. There is an option to take picture from camera and use it in the application. I am expecting:
To enable the camera from the application.
To take the picture.
To automatically close the camera and show the captured picture inside the application
Unfortunately, I am not able to get the captured image to the application. Once photo is taken, camera is not closing automatically or returning to the app. Now I have to click the back button to go to the app and select the picture manually select from SD card. Camera is opening through intent and I am using the following class.
http://developer.android.com/reference/android/hardware/Camera.html
You need to do two things. Start the Camera app and tell it where to store the picture that it takes:
File photo = new File(Environment.getExternalStorageDirectory(), "myFile.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(intent, myRequestCode);
When the user closes the Camera app, you app and Activity will be resumed. So you must override onActivityResult to get the result:
if (resultCode == myRequestCode){
File photo = new File(Environment.getExternalStorageDirectory(), "myFile.jpg");
// open it, show it, insert into MediaStore whatever
}
If you don't provide the place to save, you can alternatively retrieve it using either intent.getData or intent.getParcelableExtra using Intent.EXTRA_STREAM.
I guess .. you are doing similar to the below code
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// request code
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
using startActivityForResult instead of startActivity.. whenever you are done with your camera.. press ok.. and you will come back to your activity.. then in your activity onActivityResult callback will be called. here you will get the data for the clicked image.
Thanks.