'Media not found' when trying to play video - java

I have a weird problem, I am playing a video file from local storage, if the video name is "test#.mp4" it does not work and shows "media not found" toast, if it's "test.mp4", it works fine, no idea where is the problem.
basically if the name has "#" anywhere, the video does not play.
Here is my code
String item = names.get(itemPosition); // file name eg. test#.mp4
Uri uri = Uri.parse(context.getExternalFilesDir(null).getAbsolutePath() + "/MyFiles/"+item); // path to file
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
intent.setDataAndType(uri, "video/*");
context.startActivity(intent);

The answer is that I needed to use FileProvider, I have no idea why my earlier method was working and sometimes not.Also because the original documentation is so confusing, I used this answer, I took a look at the actual Uri and everything made sense now! android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

Related

How to open a file in external storage from an application? (Android)

I've added a button to my application which is supposed to open the download folder of the phone, and from there you should be able to click on files that were stored there, from the same app. Right now im saving some data there.
Problem is; I cant open the saved files in the folder.
I can see the files stored right there, but when I press one of them you immediatley go back to the app and not the file that you pressed.
Is there something I'm missing? Are you not supposed to open files stored in external storage from another app?
I've tried adding permissions in manifest and checkSelfpermission for checks in runtime, but with no success.
Here's the button for opening download folder:
private void openSavedLocation(){
if (ContextCompat.checkSelfPermission(ExportAndImport.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(ExportAndImport.this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
intent.setDataAndType(uri, "text/xml");
startActivity(Intent.createChooser(intent, "Open Folder"));}
I can open the file perfectly when Im opening it outside the app, not via this "createChooser". What could i be missing?
Any help is appreciated.
but when I press one of them you immediatley go back to the app and not the file that you pressed
That is what your code does. ACTION_GET_CONTENT says "let the user pick a piece of content". It does not say "open that piece of content in some other app". There is no single Intent action for saying "let the user pick a piece of content, then open that piece of content in some other app".
Is there something I'm missing?
If you want to try to open the XML in some other app:
Use startActivityForResult(), not startActivity(), for your ACTION_GET_CONTENT request (and get rid of the createChooser() bit)
Override onActivityResult() to get the result of the user's choice
If the user chose something (i.e., you get RESULT_OK in onActivityResult()), create an ACTION_VIEW Intent wrapped around the Uri that you get from the Intent passed into onActivityResult(), and call startActivity() on the ACTION_VIEW Intent
If, instead, your objective is to open this XML in your app, you would:
Use startActivityForResult(), not startActivity(), for your ACTION_GET_CONTENT request (and get rid of the createChooser() bit)
Override onActivityResult() to get the result of the user's choice
If the user chose something (i.e., you get RESULT_OK in onActivityResult()), get the Uri of the content from the Intent passed into onActivityResult(), then use ContentResolver to do something useful with that Uri (e.g., openInputStream() to read in the content)
Here's the button for opening download folder
ACTION_GET_CONTENT uses the MIME type. It will not necessarily honor your supplied starting Uri.

Android open external IMDB application from my app

I have a button in my application which opens the imdb application in the phone with a imdb id I received from https://developers.themoviedb.org/3/getting-started/introduction
But I couldnt find anyway(using intents) to make my app recognize the imdb app and open it and if imdb app do not exist then I want to open the web site. How can I accomplish this?
I think I may be able to point you in the right direction. Just to be sure, you seem to be using TMDB but wish to open in the IMDB app?
The code below is from the Android documentation.
It will start your intent if the package manager can find an app with the appropriate intent filter installed on your device. If multiple apps are able to open this intent then an app chooser should pop up, unless the user has previously set a default for this kind of URI.
Intent sendIntent = new Intent(Intent.ACTION_SEND);
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show the chooser dialog
Intent chooser = Intent.createChooser(sendIntent, title);
// Verify the original intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
If you add an else onto that then you can use a view intent like this :
Intent internetIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(movieUrl));
//Watch out here , There is a URI and Uri class!!!!
if (internetIntent.resolveActivity(getPackageManager()) != null){
startActivity(internetIntent);
}
I also found this (rather old) post about calling an explicit imdb uri
Imdb description
startActivity(android.intent.action.VIEW, imdb:///title/<titleID>);
// take note of the Uri scheme "imdb"
I hope this helps. If you post some more detail , code, links , I might be able to work through this with you.
If my answer is way off base then please be kind and set me right. We are all learning every day!
Good Luck.

Sending image to Hangouts from local folder

I'm attempting to send an image to Hangouts from within an app I'm building.
I'm working in Xamarin for VS 2015 to do this so the code below is c# but it's not much different from the equivalent Java code so I think it's easy to follow.
What I've done is set up a button on my app which has code setting up an Intent to share an image to Hangouts. I've set the image up already in the Downloads folder on the device and hardcoded the name into the code.
Intent hangoutsShareIntent = new Intent(Intent.ActionSend);
hangoutsShareIntent.SetType("image/jpeg");
hangoutsShareIntent.SetPackage("com.google.android.talk");
string downloadsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
string filePath = Path.Combine(downloadsPath, "shared.jpg");
hangoutsShareIntent.PutExtra(Intent.ExtraStream, filePath);
StartActivity(Intent.CreateChooser(hangoutsShareIntent, "Share with"));
When I run this, I get the option to select a chat in Hangouts that I want to send the content to. Upon selecting the chat, I get a blank message box and no image.
I've swapped the above code over to use text/plain and pass the filePath variable to the message. When I copy the file path into Chrome to check it, the image loads so I have to figure that the image is where I've said it is... right?
I get no errors (probably because the issue is in Hangouts rather than my app so I have nothing to debug there). Logcat shows nothing except an error I can't find much about on Google: ExternalAccountType﹕ Unsupported attribute readOnly
The only information I could find on that error implied some issue with permissions but I've made sure my app has runtime permissions checked for Read/Write using this code (which wraps the above):
if ((CheckSelfPermission(Permission.ReadExternalStorage) == (int)Permission.Granted) &&
(CheckSelfPermission(Permission.WriteExternalStorage) == (int)Permission.Granted))
NOTE: I'm running this on a HTC One M8 - no SD card but does have external storage on device. I've also added the above permissions to the manifest for earlier Android versions.
The documentation for this (here) isn't overly helpful either so any advice AT ALL here is welcome :)
Thanks!
If you use the file provider instead of sending just the URI on its own. This should get around the permission issues you are seeing.
There is a guide available here which might be useful.
Intent shareIntent = new Intent(Intent.ActionSend);
shareIntent.SetType("image/gif");
Java.IO.File file = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/myimage.gif");
Android.Net.Uri fileUri = Android.Support.V4.Content.FileProvider.GetUriForFile(this, "com.myfileprovider", file);
shareIntent.SetPackage("com.google.android.talk");
shareIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
shareIntent.PutExtra(Intent.ExtraStream, fileUri);
StartActivity(Intent.CreateChooser(shareIntent, "Share with"));

Cannot play music from local filesystem, but can play from SD card

I am developing a test app that does nothing else than play music. The idea behind this app is that normally you have a music player that takes ages to load and has all those extra gadgets and gizmos that nobody, or at least not you, uses. This app only plays music, period. And it loads almost instantly.
But I got something funny. I need to find a way for the user to select a song from the filesystem, but I haven't got that working yet, so I am using a fixed song URI to play it. When I put this song on the local filesystem(i.e. /storage/emulated/0/Music/SongName.mp3), the app crashes upon pressing the play button. But when I put the song on the SD card(i.e. /storage/extSdCard/Music/SongName.mp3) it works fine.
Well, I got it to work, but I don't know how or why it works. Normally I would be surprised and not touch that piece of code again so that it keeps working, but this time I am learning programming on Android and I want to know why it works.
This is the code for playing from the SD card(works):
p = new MediaPlayer();
p.setAudioStreamType(AudioManager.STREAM_MUSIC);
p.setDataSource(getApplicationContext(), Uri.fromFile(new File("/storage/extSdCard/Tests/Take Back The Night.mp3")));
p.prepare();
p.start();
And this is the code for playing from the local fs(does not work):
p = new MediaPlayer();
p.setAudioStreamType(AudioManager.STREAM_MUSIC);
p.setDataSource(getApplicationContext(), Uri.fromFile(new File("/storage/emulated/0/Music/heybrother.mp3")));
p.prepare();
p.start();
As you can see, it's pretty much the same, so I concluded that there is probably something wrong in the URI. But I can't see any typos, and I retyped it multiple times. Is the URI malformed for this purpose?
I got it working!
First, you need to get the path of the music folder like this:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
.getPath() + "/";
Then you can append the file name in the music folder:
String file = path + "ThatOneSong.mp3";
And then play it as normal. (Or you could do this in one long line but that's kind of cluttered.)

EXTRA_OUTPUT ignored on Eris, causes data to return null on G1

So I'm trying to launch the Camera activity using the following code:
//In public void captureImage()
...
Intent cameraIntent = new Intent(MediaStore.ACTION_CAPTURE_IMAGE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File("/sdcard/image.jpg")));
startActivityForResult(cameraIntent, REQUEST_CAMERA);
And then to handle the result:
//In onActivityResult()
...
case REQUEST_CAMERA:
Intent intent = new Intent (CurrentScreen.this, NextScreen.this);
intent.putExtra(data);
startActivity(intent);
CurrentScreen.this.finish();
...
Where I use intent.putExtra(data) to attach the small bitmap to the intent, and use it as a thumbnail in the next activity, and the full sized file is supposedly saved as /sdcard/image.jpg.
This is the expected behavior (according to the documentation), to have a small bitmap for a thumbnail, and a large file saved. However when testing this on a G1 and an Eris, I have been seeing some strange behavior.
On the G1:
Although the resultCode shows RESULT_OK, the intent data that is returned to the result handler is null.
Also EXTRA_OUTPUT seems to be completely ignored, I have no idea where it is saving the image.
On the Eris:
The intent data comes back OK
EXTRA_OUTPUT is also ignored, but it is saving the images to the regular media store at /sdcard/dcim/100media
So my question is this: is there any way to get consistent behavior for what I am trying to do using the standard camera activity? I could write up a custom activity to try and get it to work the way I want, but I'd prefer to avoid that route.
I do not have answers to your question as I am new to the Java/Android development world. But I am attempting something similar to what you are doing, except I want to simply take the picture then attach it to an Email message.
I implemented part of your example and was able to verify that the camera created the file I specified and that if I use the same file name for the next picture that it overwrites the previous file which is what I would expect.
But what I was really going to say is perhaps you will have to test if the pat "/sdcard/..." actually exists or not. Also you could possibly simplify your process by passing the path to the next activity.
Good Luck,
Jamie Irwin

Categories