android- open gallery and choose image and video - android

In my project I want to open a gallery on a button click and should be able to pick image or video to get path of them.
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
From above code i am able to open gallery but in this case i am only able to choose image. So, please help me in choosing video also.
Thanks in advance.

On Android 6.0 and above using "video/* image/" or "image/ video/*" type doesn't work, it only recognizes the first filter you specify. I solved the problem using this code:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("*/*");
photoPickerIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});
startActivityForResult(photoPickerIntent, Constants.SELECT_PHOTO);
Although this will ask the user which app they want to use to select the image/video.

You can use the next snippet:
Intent mediaChooser = new Intent(Intent.ACTION_GET_CONTENT);
//comma-separated MIME types
mediaChooser.setType("video/*, image/*");
startActivityForResult(mediaChooser, RESULT_LOAD_IMAGE);
But I think that it only work on ICS or bigger

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/*");

Below code solved my problem
final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("*/*");
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);

2022 Android 9
I've tried everything available online, and accidently mixed 2 solutions that turned out to work.
This only gives photo library and google photos as options, and you can select photos AND videos.
libraryIntent.setType("video/*, image/*");
String[] mimetypes = {"image/*", "video/*"};
libraryIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);

Not enough rep to comment, but #YYamil's response works well.
Intent mediaChooser = new Intent(Intent.ACTION_GET_CONTENT);
//comma-separated MIME types
mediaChooser.setType("video/*, image/*");
startActivityForResult(mediaChooser, RESULT_LOAD_IMAGE);
If you're using the new registerForResultActivity, create a copy of ActivityResultContracts.GetMultipleContents() and put in createIntent:
#CallSuper
override fun createIntent(context: Context, input: String): Intent {
return Intent(Intent.ACTION_GET_CONTENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType(input)
.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
.putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*", "video/*")) // this does the trick
}

This is the best i known...... Try this for once....
final CharSequence[] options = {"Images", "Videos", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(OpenGallery.this);
builder.setTitle("Select From...");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Images")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 1);
} else if (options[item].equals("Videos")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 1);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
dialog.dismiss();
}
});
builder.show();

Change your Intent to this:
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
When trying to get videos you need to state to mediaStore that video is in order and not images as you wrote.

Well android has put a lot of restriction on accessing the external content. I ended up using 3rd party library. This one is good.:
https://github.com/AnilFurkanOkcun/UWMediaPicker-Android
implementation 'com.github.AnilFurkanOkcun:UWMediaPicker-Android:1.3.0'
UwMediaPicker
.with(this) // Activity or Fragment
.setGalleryMode(UwMediaPicker.GalleryMode.ImageGallery) // GalleryMode: ImageGallery/VideoGallery/ImageAndVideoGallery, default is ImageGallery
.setGridColumnCount(4) // Grid column count, default is 3
.setMaxSelectableMediaCount(10) // Maximum selectable media count, default is null which means infinite
.setLightStatusBar(true) // Is llight status bar enable, default is true
.enableImageCompression(true) // Is image compression enable, default is false
.setCompressionMaxWidth(1280F) // Compressed image's max width px, default is 1280
.setCompressionMaxHeight(720F) // Compressed image's max height px, default is 720
.setCompressFormat(Bitmap.CompressFormat.JPEG) // Compressed image's format, default is JPEG
.setCompressionQuality(85) // Image compression quality, default is 85
.setCompressedFileDestinationPath(destinationPath) // Compressed image file's destination path, default is "${application.getExternalFilesDir(null).path}/Pictures"
.launch{selectedMediaList-> } // (::onMediaSelected) // Will be called when media is selected

Related

How to pick only videos from gallery?

I want to be able to pick only videos from gallery but I only found tutorials that select images but I only want to be able to select videos.
Can anyone help?
For example, when you use an intent, you can set the type of your intent. Here I'm using "video/*" to get all videos of my device.
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("video/*");
startActivity(galleryIntent);
Try this code..
protected int REQUEST_TAKE_GALLERY_VIDEO = 3;
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Video"),REQUEST_TAKE_GALLERY_VIDEO);

Android: make internal file picker available as a choice

When opening a file picker with the following method:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent chooserIntent = Intent.createChooser(intent, "Open file");
startActivityForResult(chooserIntent, REQUEST_CODE_FILE_PICKER);
unless a default has been set, this will present the user with a choice of file pickers to use. How do you make your own, internal, file chooser available as one of the choices of file pickers presented to the user (such as the Material File Picker)?
Thanks to #CommonsWare's comment, here is the extra bit of code needed to add the internal file picker to the list of choices presented to the user:
// this bit is as before...
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent chooserIntent = Intent.createChooser(intent, "Open file");
// new bit... create Intent for the internal file picker...
Intent materialFilePickerIntent = new Intent(this, FilePickerActivity.class);
materialFilePickerIntent.putExtra(FilePickerActivity.ARG_FILE_FILTER, Pattern.compile(".*\\.txt$"));
// and add the picker to the list...
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { materialFilePickerIntent });
// finally, startActivityForResult() as before...
startActivityForResult(chooserIntent, REQUEST_CODE_FILE_PICKER);

Correct intent to show only Audio files

I am trying to pick Audio from Gallery. I am using the following intent.
private void selectAudioFromGallery() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/3gp, audio/AMR, audio/mp3");
startActivityForResult(Intent.createChooser(intent, getString(R.string.select_audio_file_title)), REQ_CODE_PICK_SOUNDFILE);
}
I want to select files of type 3gp, AMR, mp3 and only these files should be listed in the file chooser but the problem I am facing it, it also shows the other files too like text files, image etc.
Intent intent = null;
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
}else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
}
// The MIME data type filter
intent.setType("audio/*")
Works..
Use this
intent.setType("audio/3gp|audio/AMR|audio/mp3");
Adding multiple mime type need pipe sign, not comma.
Another way
intent.setType("audio/*");
String[] mimetypes = {"audio/3gp", "audio/AMR", "audio/mp3"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);

Gallery intent shows only image or video not both?

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);

Pick any kind of file via an Intent in Android

I would like to start an intentchooser for apps which can return any kind of file
Currently I use (which I copied from the Android email source code for file attachment)
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Intent i = Intent.createChooser(intent, "File");
startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);
But it only shows "Gallery" and "Music player" on my Galaxy S2. There is a file explorer on this device and I would like it to appear in the list. I would also like the camera app to show in the list, so that the user can shoot a picture and send it through my app.
If I install Astro file manager it will respond to that intent, too. My customers are Galaxy SII owners only and I don't want to force them to install Astro file manager given that they already have a basic but sufficient file manager.
Any idea of how I could achieve this ? I am pretty sure that I already saw the default file manager appear in such a menu to pick a file, but I can't remember in which app.
Not for camera but for other files..
In my device I have ES File Explorer installed and This simply thing works in my case..
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
Samsung file explorer needs not only custom action (com.sec.android.app.myfiles.PICK_DATA),
but also category part (Intent.CATEGORY_DEFAULT) and mime-type should be passed as extra.
Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
You can also use this action for opening multiple files: com.sec.android.app.myfiles.PICK_DATA_MULTIPLE
Anyway here is my solution which works on Samsung and other devices:
public void openFile(String mimeType) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(mimeType);
intent.addCategory(Intent.CATEGORY_OPENABLE);
// special intent for Samsung file manager
Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
// if you want any file type, you can skip next line
sIntent.putExtra("CONTENT_TYPE", mimeType);
sIntent.addCategory(Intent.CATEGORY_DEFAULT);
Intent chooserIntent;
if (getPackageManager().resolveActivity(sIntent, 0) != null){
// it is device with Samsung file manager
chooserIntent = Intent.createChooser(sIntent, "Open file");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});
} else {
chooserIntent = Intent.createChooser(intent, "Open file");
}
try {
startActivityForResult(chooserIntent, CHOOSE_FILE_REQUESTCODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
}
}
This solution works well for me, and maybe will be useful for someone else.
this work for me on galaxy note
its show
contacts,
file managers installed on device,
gallery,
music player
private void openFile(Int CODE) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
startActivityForResult(intent, CODE);
}
here get path in onActivityResult of activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String Fpath = data.getDataString();
// do somthing...
super.onActivityResult(requestCode, resultCode, data);
}
This gives me the best result:
Intent intent;
if (android.os.Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
} else {
String[] mimeTypes =
{"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
"application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
"text/plain",
"application/pdf",
"application/zip", "application/vnd.android.package-archive"};
intent = new Intent(Intent.ACTION_GET_CONTENT); // or ACTION_OPEN_DOCUMENT
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
}
Turns out the Samsung file explorer uses a custom action. This is why I could see the Samsung file explorer when looking for a file from the samsung apps, but not from mine.
The action is "com.sec.android.app.myfiles.PICK_DATA"
I created a custom Activity Picker which displays activities filtering both intents.
If you want to know this, it exists an open source library called aFileDialog that it is an small and easy to use which provides a file picker.
The difference with another file chooser's libraries for Android is that aFileDialog gives you the option to open the file chooser as a Dialog and as an Activity.
It also lets you to select folders, create files, filter files using regular expressions and show confirmation dialogs.
The other answers are not incorrect. However, now there are more options for opening files. For example, if you want the app to have long term, permanent acess to a file, you can use ACTION_OPEN_DOCUMENT instead. Refer to the official documentation: Open files using storage access framework. Also refer to this answer.

Categories

Resources