How to pick image or video on Android L? - android

I am using below code and it works fine below android 5. I am able to pick image or video from SD card.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/* image/*");
getActivity().startActivityForResult(photoPickerIntent, 1);
However on Android L it shows only videos.
tried searching but didn't find anything, any help will be appreciated.

hi #Mohit you can use this solution for image and video
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("*/*");
getActivity().startActivityForResult(photoPickerIntent, 1);
for both image and video you can use setType(*/*);
here ACTION_GET_CONTENT is give only gallery selection while ACTION_PICK give many more options to pick image and video from different action, So as per #DipeshDhakal answer you should use only ACTION_GET_CONTENT.
and this is work on android L and api 10 also.

Use Intent.ACTION_GET_CONTENT
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("video/*, images/*");
startActivityForResult(photoPickerIntent, 1);

Was running into a similar issue. Code that worked on 5.0 and below started breaking on 5.1+, and only filtered by the first type passed in.
A co-worker came up the following, and I thought I'd share:
We were previously using the following intent:
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*,video/*");
and the following code to get the path from whatever the user selected, if anything:
public static String getFilePathFromURI(Uri imageUri, Activity context) {
String filePath = null;
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(imageUri,
filePathColumn, null, null, null);
if (cursor != null) {
try {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
} finally {
cursor.close();
}
}
return filePath;
}
Now:
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
String[] mimetypes = {"image/*", "video/*"};
i.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
And to get the path, the getPath function found in this answer:
https://stackoverflow.com/a/20559175/434400

Related

Android open gallery from folder

I want to show a photo in android gallery, and be able to slide throw the others photos on that folder.
Intent intent = new Intent(Intent.ACTION_VIEW);
File f = new File(path);
intent.setDataAndType(Uri.parse("file://" + f.getAbsolutePath()), "image/*");
mContext.startActivity(intent);
thats how i am doing it now, but wont let me slide throw the rest of the images in the folder.
i tried:
How to open one particular folder from gallery in android?
Built-in gallery in specific folder
Gallery with folder filter
Without any luck.
i would be really happy if someone have the solution.
Thanks!
Try This
Intent i=new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(path)), "image/*");
startActivity(i);
See these Links
How can I use Intent.ACTION_VIEW to view the contents of a folder?
Android ACTION_VIEW Multiple Images
Java and Android: How to open several files with an Intent?
if this solves your problem. Also check
https://www.google.co.in/?gfe_rd=cr&ei=c5n9U6ruE7DO8gfXz4G4BA&gws_rd=ssl#q=view+like+gallery
also check Gallery widget
This question is from five years ago, However I want to give an answer that worked for me instead of the correct answer.
In order to show the photo and slide through the others photos, we need to give to the intent not the file uri but the media uri.
public void ShowPhoto(File imageFile) {
String mediaId = "";
String[] projection = new String[] {
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME
};
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null, null);
while (cursor != null && cursor.moveToNext()) {
String name = cursor.getString((cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME)));
if(name.equals(imageFile.getName())){
mediaId = cursor.getString((cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)));
break;
}
}
Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if(!mediaId.equals("")){
mediaUri = mediaUri.buildUpon()
.authority("media")
.appendPath(mediaId)
.build();
}
Log.d("TagInfo","Uri: "+mediaUri);
Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
startActivity(intent);
}
Try this way,hope this will help you to solve your problem.
final int OPEN_GALLERY = 1
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), OPEN_GALLERY);

Start the trim video activity with an intent

I can take a video with an intent now what are the details to create an intent to start the default video trimmer activity? And check if it present on the device?
This solution relies on a version of the AOSP Gallery2 package being installed on the device. You can do it like this:
// The Intent action is not yet published as a constant in the Intent class
// This one is served by the com.android.gallery3d.app.TrimVideo activity
// which relies on having the Gallery2 app or a compatible derivative installed
Intent trimVideoIntent = new Intent("com.android.camera.action.TRIM");
// The key for the extra has been discovered from com.android.gallery3d.app.PhotoPage.KEY_MEDIA_ITEM_PATH
trimVideoIntent.putExtra("media-item-path", getFilePathFromVideoURI(this, videoUri));
trimVideoIntent.setData(videoUri);
// Check if the device can handle the Intent
List<ResolveInfo> list = getPackageManager().queryIntentActivities(trimVideoIntent, 0);
if (null != list && list.size() > 0) {
startActivity(trimVideoIntent); // Fires TrimVideo activity into being active
}
The method getFilePathFromVideURI is based on the answer of this question: Get filename and path from URI from mediastore
public String getFilePathFromVideoURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Video.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
videoUri is an Uri pointing to something like this: content://media/external/video/media/43. You can gather one by issuing an ACTION_PICK Intent:
Intent pickVideoUriIntent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickVideoUriIntent, PICK_VIDEO_REQUEST);
In onActivityResult get the uri like so:
....
case PICK_VIDEO_REQUEST:
Uri videoUri = data.getData();
...
This solution works on my Galaxy Nexus with Android 4.3 Jelly Bean.
I am not sure if this is available on all Android devices.
A more reliable solution may be to fork the Gallery2 app and put the TrimVideo activity together with its dependencies into a library that can be delivered with your app.
Hope this helps anyway.
Try this may it helps
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra("android.intent.extra.durationLimit", 30000);
intent.putExtra("EXTRA_VIDEO_QUALITY", 0);
startActivityForResult(intent, ActivityRequests.REQUEST_TAKE_VIDEO);
This code works well on API >=2.2, but the duration limit does not work on API 2.1

URI/URL Paths from Gallery and Camera-taken-photo are different

I am trying to get the users to select between taking a picture with the device default camera and select from the gallery of images also default to the device.
I can get the camera to take the picture and have it display within the app just fine since it seems to like the URI pathing straight to a JPG file. However, the pathing given to the gallery URI is very different and does not display the image at all.
Here are the pathes I get:
WHEN TAKEN FROM CAMERA:
/mnt/sdcard/filename.jpg
WHEN CHOSEN FROM GALLERY:
/external/images/media/# (# is the ID number/thumbnail I believe)
The code used for retrieving both pathes are:
CAMERA:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri =
Uri.fromFile(new file(Environment.getExternalStorageDirectory(),
"fname_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
GALLERY:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_FILE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
With the Gallery, it opens and I can browse just fine, it just doesn't display the image as it does with taking the picture.
The code used for pulling the images into my app once selected/taken is:
ImageView getMyphoto = (ImageView) findViewById(R.id.usePhoto);
String stringUrl = prefSettings.getString("myPic", "");
Uri getIMG = Uri.parse(stringUrl);
getMyphoto.setImageURI(null);
getMyphoto.setImageURI(getIMG);
Check for the "/external" in the uri string and then use get the right path method to get the absolute path.
private String getRealPathFromURI(Uri contentUri) {
int columnIndex = 0;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
try {
columnIndex = cursor.getColumnIndexOrThrow
(MediaStore.Images.Media.DATA);
} catch (Exception e) {
Toast.makeText(ImageEditor.this, "Exception in getRealPathFromURI",
Toast.LENGTH_SHORT).show();
finish();
return null;
}
cursor.moveToFirst();
return cursor.getString(columnIndex);
}

Android: Let user pick image or video from Gallery

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

how can i get real path in image gallery?

I got all images from device's gallery,but i got images path is like content://media/external/image/media/102,so i want to get real image path for each images and send email,how can i get path?given below code i used,anybody knows,please give some sample code for me..i have email code.i want to only get real image path.How can I convert this path to real one (just like '/sdcard/image.png') ?
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
Uri photoUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor actualimagecursor = managedQuery(photoUri, proj,null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
it work well..

Categories

Resources