Playing audio from intent - ActivityNotFoundException - android

I am trying to play audio with the ACTION_VIEW intent, but regardless on what type of sound file (.mp3, .3gpp etc.) I always get the same error:
E/AndroidRuntime(3007): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/mnt/sdcard/recording-23363382.3gpp typ=audio/3gpp }
My code:
Firstly here is where I launch the activity:
case R.id.largeThumbnailText:
media_column_index = mediacursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
mediacursor.moveToPosition(lastClicked);
mediaFileUri = Uri.parse(mediacursor.getString(media_column_index));
media_column_index = mediacursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE);
String mime = mediacursor.getString(media_column_index);
Intent playAudioIntent = new Intent();
playAudioIntent.setAction(Intent.ACTION_VIEW);
playAudioIntent.setDataAndType(mediaFileUri, mime);
startActivity(playAudioIntent);
break;
This is where I initiate the mediacursor:
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.MIME_TYPE };
mediacursor = getActivity().managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null,
null, null);
I do the same thing with video, so I don't see where the problem is. Might be worth mentioning that this is all done from within a fragment.
Forgot to mention I'm testing it on a HTC Sensation (android 2.3.4)
Appreciate any suggestions

Instead of your code
Intent playAudioIntent = new Intent();
playAudioIntent.setAction(Intent.ACTION_VIEW);
playAudioIntent.setDataAndType(mediaFileUri, mime);
startActivity(playAudioIntent);
Try something like this:
String movieurl = Environment.getExternalStorageDirectory() + "/Videos/Wildlife.wmv";
Intent playAudioIntent = new Intent(Intent.ACTION_VIEW);
playAudioIntent .setDataAndType(Uri.parse(movieurl), "video/*");

common error when setting wrong mime type in the intent take this example check if it is audio/mp3 to play mp3 audios
Uri data = Uri.parse("file:///sdcard/abc_xyz.mp3");
intent.setDataAndType(data,"audio/mp3");
startActivity(intent);

Related

How to pick image or video on Android L?

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

Share video to viber via intents in android not working

I googled a lot but no success.
I want to share a video file in my App via this code:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("video/mp4"); //or even video mpeg not working!
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + outputFileInformation.getFullPath()));
startActivity(Intent.createChooser(intent, getString(R.string.share)));
And I tried some other codes, But for in sharing list if I choose Viber, After selecting recipient nothing happens, Screen flashes and nothing happens.
(I must say sharing image/png has no issue.
I really need to get this work soon.
I could easily share that video from gallery without any issue but now working in my app... .
I manged to make it work.
I must make an Uri in another way, here is the code i used for making Uri from a video file path (you could change it for Images as well)
public static Uri getVideoContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Video.Media._ID },
MediaStore.Video.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/video/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}

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

how to open native contact card by URI

Can someone please explain me how to open contact native for specific contact URi
I manage to resolve the contact URI by:
Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(displayName));
and then I tried do this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT, "555");
context.startActivity(intent);
but it's only open the native address book without any resolving
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
intent.setData(uri);
context.startActivity(intent);
You can retrieve the contactId by browsing using contentResolver:
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d(tag, "Id: "+ id+"\t Name: "+name);
After you get the cursor of contacts and the index of a particular contact, get the Uri and start activity with intent of ACTION_VIEW as below:
cursor.moveToPosition(position); // or cursor.moveToFirst() if single contact was selected.
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(ContactsContract.Contacts.getLookupUri(id, lookupKey));
try {
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
The accepted answer does not work when Android Messages is not the user's default SMS app. For some reason the intent goes through Android Messages before opening the contact details, and when the app is not default it takes you to their SMS permissions activity.
The below intent uses a slightly different URI: CONTENT_LOOKUP_URI instead of CONTENT_URI, and it works no matter which texting app the user is using.
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, String.valueOf(contactID));
intent.setData(uri);
context.startActivity(intent);

Categories

Resources