Contents
Mobile App Development: Android Media Picker Library To Pick Photos And Videos
In your Android application, have you ever written code to allow your users to pick photos & videos from devices via gallery app or camera app? If you have, you might have encountered various different nasty bugs when you tried to get them to work with many Android devices?This is because there are many Android devices from many mobile phone manufacturers and the gallery logic or camera logic are customized by the manufacturers.
In this tutorial, we will show you a solution to deal with this problem and introduce an Android Media Picker Library which helps you pick photos and videos conveniently.
Tung Dao Xuan, tungdx1210@gmail.com, is the author of this article and he contributes to RobustTechHouse Blog
Video
Source Code
You can find the source code at GitHub.
Solution – Android Media Picker Library
Where possible, you should avoid working directly with gallery app because it depends on the manufacturer. Instead, you should work with the Media Store provider in Android which is independent of the manufacturer. This content provider contains meta data for all available media on both internal and external storage devices.
For more details on Content Providers in Android, please check out this link Android Content Providers.
Here are some of the features of the Android Media Picker Library we will show here:
- Allows you to pick one or multiple photos or videos
- Options to crop photo.
- Options for pick video.
- Use with Activity or Fragment.
How to use Android Media Picker Library
Step 1: Add library
- Add MediaPicker library to your project.
- Add MediaPickerActivity into AndroidManifest.xml of your project.
<activity android:name="vn.tungdx.mediapicker.activities.MediaPickerActivity" android:screenOrientation="portrait" android:theme="@style/MediaPickerTheme" />
Step 2: Start activity to pick photos or videos
Use the methods in MediaPickerActivity.java to pick photos or videos
MediaPickerActivity.open(Activity activity, int requestCode, MediaOptions options); MediaPickerActivity.open(Activity activity, int requestCode); MediaPickerActivity.open(Fragment fragment, int requestCode, MediaOptions options); MediaPickerActivity.open(Fragment fragment, int requestCode);
Eg: Start activity to pick photo with crop options.
private static final int REQUEST_MEDIA = 100; MediaOptions.Builder builder = new MediaOptions.Builder(); MediaOptions options = builder.setIsCropped(true).setFixAspectRatio(true).build(); MediaPickerActivity.open(this, REQUEST_MEDIA, options);
Eg: Start activity to pick multiple videos.
private static final int REQUEST_MEDIA = 100; MediaOptions.Builder builder = new MediaOptions.Builder(); MediaOptions options = builder.selectVideo().canSelectMultiVideo(true).build(); MediaPickerActivity.open(this, REQUEST_MEDIA, options);
To find out more detail about options available, you can look into the MediaOptions.Builder.java class.
Step 3: Receive photos or videos
Override onActivityResult(int requestCode, int resultCode, Intent data) method in your Activity or Fragment that pick photos or videos by using code below.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_MEDIA) { if (resultCode == RESULT_OK) { List<MediaItem> mediaSelectedList = MediaPickerActivity .getMediaItemSelected(data); } } }
To get information from the item selected, you can call the following:
MediaItem.getUriCropped() MediaItem.getUriOrigin() MediaItem.getPathOrigin(Context context) MediaItem.getPathCropped(Context context)
To know more details, you can check out MediaItem.java class.
Conclusion
Android platforms are very fragmented which makes it really challenging when you are designing your Android app. In your applications, you should avoid using apps and components that depend on specific manufacturers. Hope this tutorial is useful for you and as a developer and hopefully you will find it easier to write code to allow your app users to pick photos, videos on Android with our Android Media Picker Library.
Happy coding!
RobustTechHouse is a leading tech company focusing on mobile app development in Singapore. If you are interested to engage RobustTechHouse on your projects, you can contact us here.
Hey how to import this project library in to android studio?
i am seeing new dress for girls
Shop the cutest baby clothes online. Find a variety of lovely clothing options for your children, including dresses, and accessories. Find the ideal clothing for each occasion. Shop now!