I have to select video or image on chooser intent like this
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/* video/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK);
pickIntent.setType("image/* video/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Select Media");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
startActivityForResult(chooserIntent, 1);
Then I want to get the metadata of the file chosen in onActivityResult()
like height and width and length of the video and the size
Use this to get all your requirements.
MediaMetadataRetriever ret = new MediaMetadataRetriever();
Bitmap bmp = null;
try
{
ret.setDataSource("<<--Your retrieved data file-->>");
bmp = ret.getFrameAtTime();
videoHeight=bmp.getHeight();
videoWidth=bmp.getWidth();
videoSize = bmp.getAllocationByteCount()
}
Hope this helps, and let me know if any issue arises.
Related
I need to call Intent for image and video both at same time . It seems impossible similar to this . So a alternate way to do this is createChooser() . But i am kinda stuck with the code below .
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, file.getAbsolutePath());
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, file.getAbsolutePath());
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
Intent[] intentArray = new Intent[]{takePictureIntent};
chooserIntent.putExtra(Intent.EXTRA_INTENT, takeVideoIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Choose an action");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, 1);
MediaStore.EXTRA_OUTPUT not working . The file is empty after returning from camera . However it does open a chooser as below but file is empty .
Questions:-
1.How to can i combine both intents and provide separate files as MediaStore.EXTRA_OUTPUT?
2. As we all know there are hundreds of camera apps in android so is this a good way to open such intent(Will it work in all devices regardless of manufacturer and API Level) ? Or i should move with an AlertDialog to open intent for each action seperatly .
Try this below code :
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Intent chooserIntent = Intent.createChooser(takePictureIntent, "Capture Image or Video");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takeVideoIntent});
startActivityForResult(chooserIntent, CAMERA_IMAGES_REQUEST);
I am trying to allow a user to choose an image or video from his device, and currently it only shows video or image depending what is written first in the following code:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//set type to include video too
galleryIntent.setType("image/*, video/*");
startActivityForResult(galleryIntent, GALLERY_IMAGE_REQUEST_CODE);
}
};
not sure what I am doing wrong, but setType seems right I tried with and without the comma in between image and video...
case 2: //Choose Pic
Intent choosePhotoIntent = new Intent(Intent.ACTION_GET_CONTENT);
choosePhotoIntent.setType("image/*");
startActivityForResult(choosePhotoIntent, PICK_PHOTO_REQUEST);
break;
case 3: //Choose Video
Intent chooseVideoIntent = new Intent(Intent.ACTION_GET_CONTENT);
chooseVideoIntent.setType("video/*");
Toast.makeText(MyActivity.this,getString(R.string.video_message), Toast.LENGTH_LONG).show();
startActivityForResult(chooseVideoIntent, PICK_VIDEO_REQUEST);
break;
Try above..you need to have separate options..
I ran into the same issue where it would only use the first MIME type in the list.
This ended up working for me:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("*/*");
String[] mimeTypes = {"image/*", "video/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, REQUEST_CODE_CAMERA_ROLL);
This works for me:
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_MEDIA);
Is there an Intent for starting a camera with options to capture both Pictures and Videos on Android?
I've used both MediaStore.ACTION_VIDEO_CAPTURE and MediaStore.ACTION_IMAGE_CAPTURE to capture either audio or video, but I can't find an Intent that will get the option for switching between both of them, as in this example app:
Thanks!
It is not possible to capture both image and video using the same intent, Your options are
1) Create your own camera this repo can be a good start But it is going to be a too much effort.
2) Use the Chooser Intent and pass the intent for both image and video, this will give you the option to choose between application which record video and camera separately. In this you cannot do both the things at same time but can choose application according to what you want to do, capture an image or record a video. Below is the code that works for me.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Intent chooserIntent = Intent.createChooser(takePictureIntent, "Capture Image or Video");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takeVideoIntent});
startActivityForResult(chooserIntent, CAPTURE_MEDIA_RESULT_CODE);
I achieved it :)
You can do it by following --
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("*/*");
intentArray = new Intent[]{takePictureIntent,takeVideoIntent};
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Choose an action");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, 1);
Similar example here
Happy coding :)
I could capture both image and video by using the below code.
Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
I want to do a intent to the gallery for take a photo. It's easy to find a lot of examples that do this, but i want that open the gallery in my own album directly. it's possible?
You can try doing something like this:
private static final int SELECT_PICTURE = 1;
// ...
Intent in = new Intent();
in.setType("image/*");
in.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
Intent chooserIntent = Intent.createChooser(in, pickTitle);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent }
);
startActivityForResult(chooserIntent, SELECT_PICTURE);
Is it possible to to start Gallery in such a way so both pictures and videos are shown?
Thanks
Pick Audio file from Gallery:
//Use MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
Pick Video file from Gallery:
//Use MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
Pick Image from gallery:
//Use MediaStore.Images.Media.EXTERNAL_CONTENT_URI
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Pick Media Files or images:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/* video/*");
You start the gallery as such:
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/* video/*");
startActivityForResult(pickIntent, IMAGE_PICKER_SELECT);
then in your onActivityResult you can check if video or image was selected by doing this:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedMediaUri = data.getData();
if (selectedMediaUri.toString().contains("image")) {
//handle image
} else if (selectedMediaUri.toString().contains("video")) {
//handle video
}
}
(EDIT: I don't use it anymore, we went back to the two choices "pick image" and "pick video". The problem was with some Sony phone. So, it's not 100% solution below, be careful! )
This is what I use:
if (Build.VERSION.SDK_INT < 19) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/* video/*");
startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)), SELECT_GALLERY);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});
startActivityForResult(intent, SELECT_GALLERY_KITKAT);
}
The key here is intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});
intent.setType("*/*");
This presents user with dialog but works on at least ICS. Haven't tested on other platforms.
When you need to determine what kind of content was returned, you can do it using content resolver to get the MIME type of the returned content:
if( data != null) {
Uri selectedUri = data.getData();
String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media.MIME_TYPE };
Cursor cursor = getContentResolver().query(selectedUri, columns, null, null, null);
cursor.moveToFirst();
int pathColumnIndex = cursor.getColumnIndex( columns[0] );
int mimeTypeColumnIndex = cursor.getColumnIndex( columns[1] );
String contentPath = cursor.getString(pathColumnIndex);
String mimeType = cursor.getString(mimeTypeColumnIndex);
cursor.close();
if(mimeType.startsWith("image")) {
//It's an image
}
else if(mimeType.startsWith("video")) {
//It's a video
}
}
else {
// show error or do nothing
}
CoolIris which came with my galaxy tab can do it. However the cooliris on my acer betouch will not :S
On my milestone you can not start the gallery with a pick intent on the video url however when you start it on the images url, you can select a video and it will return a video url too.
UPDATE 2021
FINALLY a solution working for Android 9.
This piece of code only open image apps, and you can select both images and videos. I tried a bunch of different combinations and this exact code will make it work.
libraryIntent.setType("video/*, image/*");
String[] mimetypes = {"image/*", "video/*"};
libraryIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
Still Working On Jan'2022
If This is Working For You Then Try it,
Intent intent = new Intent(Intent.ACTION_PICK, android.provider
.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/* video/*");
startActivityForResult(intent,PICK_FILE);
else For Older SDK's and For Some Devices Try the below one,
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});
startActivityForResult(intent,PICK_FILE);
where, PICK_FILE is a variable,
private static final int PICK_FILE = 1;
This is working for me for Android 12 (SDK 32)
Pick multiple images & videos from the gallery
Also with the latest registerForActivityResult
val resultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
onActivityResult(result)
}
fun pickMediaFiles() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/* video/*"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*", "video/*"))
resultLauncher.launch(intent)
}
fun onActivityResult(result: ActivityResult) {
if (result.resultCode == RESULT_OK && result.data != null) {
//If selected multiple medias
if (result.data?.clipData != null) {
val count: Int =
result.data!!.clipData!!.itemCount
for (i in 0 until count) {
val selectedUri: Uri? = result.data!!.clipData?.getItemAt(i)?.uri
}
}
//If selected single media
else if (result.data?.data != null) {
val selectedUri: Uri? = result.data?.data
}
}
}
You need use the following as picking Intent
Intent photoLibraryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoLibraryIntent.setType("image/* video/*");