DONT mark this as duplicate, before reading what I need.
I have seen many similar topics, but in none of them I've found solution.
I need the simplest thing: In my application I have button "View Media Files". After clicking that button, i need to be opened (with built-in File Explorer) this directory - SD_CARD/my_folder where are media files (and I want to click any of them and they should be opened in default Media player)..
I have used all suggested answers on SO , like this:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri mydir = Uri.parse("/sdcard/Recorder_Videos");
intent.setDataAndType(mydir, "*/*");
startActivity(intent);
but all they do: after clicking button, it opens "Choose File" menu:
(Where I cant still play media files when clicking)
The solution (not complete) I have found, was that I was missing file:// prefix. Here is my solution (however, it shows all kinds of applications on first view):
public void openFolder(String location)
{
// location = "/sdcard/my_folder";
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri mydir = Uri.parse("file://"+location);
intent.setDataAndType(mydir,"application/*"); // or use */*
startActivity(intent);
}
p.s. Strange and surprising, but there doesnt exist the standard definition of "File Browser" in stock Android systems (unless you install 3rd party "File Explorer")..
That's why "resource/folder" mime-type doesnt work by default..
However, let's say a simple truth. File-Browser is a SIMPLE and ESSENTIAL part of any OS system. And it's quite disrespectful from Android, saying that it's not their job, and throwing the responsiblity to 3rd party apps.
You can use type DocumentsContract.Document.MIME_TYPE_DIR which works on several devices and launches File Explorer. You can refer this SO for more details.
Related
I tried picking a file from the internal or external storage with the code below:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, 1);
Of course it has onActivityResult method, and it's not the problem. It works fine in the modern phones or phones that have file manager installed. But the old one with no file manager throws
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT typ=file/* }
I tried switching to ACTION_PICK but no luck. I also tried intent.setType("*/*");, it didn't crash but the popup ask for action (videos, contacts,...) which is not true. I just want to pick any file not just a specified type.
I don't want to use any other file manager just to pick a file. Is there anyway I can get through this?
I believe that having the error explained, makes the solution much easier. So let me explain it to you:
You're starting an implicit intent. That means it's an intent that you know what you want to happen (use select a file) and you don't care which application will do it.
The error you're encountering is simply the system telling you (the developer), that there's no application installed that is capable of doing it (neither system nor 3rd party). There's simply no one capable of handling the action you want.
So you have two options from what I can see:
try-catch the error
.
try {
startActivityForResult(intent, 1);
} catch (ActivityNotFoundException e) {
// maybe you should show a toast to the user here?
Toast.makeText(context, "You need to install a file picker", Toast.LENGTH_SHORT).show();
// or maybe redirect to a 3rd party app that you know works
startIntent(new Intent(Uri.parse("https://play.google.com/... some app
}
you can find a library or code to pick the file from inside your own app: http://bit.ly/1N1fZbO
Below code section should work for you!
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
Intent intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
What's your Android version? Some android maybe not released with such activity.
As always, this is a bit difficult to explain so I hope you can all follow.
I have an Activity that allows people to search through their file system for a file they'd like to upload to a server. What I want, is for when a person clicks a file, that it'll open the file so the user can see if it is the right one/edit it/ect and then click accept to send it or decline to return to the file system search.
Here is what I have so far:
File selectedFile = new File(o.getPath());
Uri fileUri = Uri.fromFile(selectedFile);
Intent i = new Intent(FileChooser.this, FileView.class);
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(selectedFile), "text/*");
startActivity(i);
I'm able to open the files no problem when I don't try to call FileView.class, of
course. But then I'm not able to add the menu...I feel like there's something simple I'm missing.
Anyone have any experience doing this?
I am experimenting with Android development. I am making an app that will allow the user to browse files in a web service and view them. These files could be anything: text, pdf, pictures, etc.
Previously, I would download the file to external storage and then call Intent.SetDataAndType() and pass it the URL to the file. That would cause the Android device to bring up an app picker and let the user choose the appropriate method to look at the file.
But since I do not want the user to edit the file, only to look at it, it seemed silly to download a file to storage; a file that I didn't want to hang around. Since the file can be obtained by a URL, why don't I pass that as a parameter to the Intent.SetDataAndType()?
I tried that. The first problem was that the file name was assumed to be the name of the web service call, and that seemed to be more important than the mime-type. I changed the web service to be the same name as whatever file was attempting to be downloaded. That solved that issue.
So now, the file is being opened. But it is always being opened in a web browser. I get to choose the web browser, but I would rather have another app open it.
My code looks like this:
Intent i = new Intent(Intent.ActionView);
i.SetDataAndType(Android.Net.Uri.Parse(GetUrlToFile(fileref, fileName)), mimeType);
i.SetFlags(ActivityFlags.GrantReadUriPermission);
i.SetFlags(ActivityFlags.NewTask);
i.SetFlags(ActivityFlags.ClearWhenTaskReset); // so if the app is relaunched, we don't show the display application.
StartActivity(i);
The code is in C# because I'm using Xamarin, but I don't believe that should make a difference.
I tried using StartActivity(Intent.CreateChooser(i, "Open me")); but that didn't give me any more options for choosing.
Does anyone have any ideas as to how to do this?
I have not found a way to do this yet, so I have gone through a workaround.
Instead of using a URL, I changed my app to be a Content Provider as well. Now, when I want the file opened, I create a URI that refers to the file within my app and pass that off to an Intent. When my app is contacted by this Intent, I download the file locally to my cache directory and return that.
My code has changed to this:
Intent i = new Intent(Intent.ActionView);
i.SetDataAndType(Android.Net.Uri.Parse("content://com.sample.erik.provider/files/" + id), mimeType);
i.SetFlags(ActivityFlags.GrantReadUriPermission);
i.SetFlags(ActivityFlags.NewTask);
i.SetFlags(ActivityFlags.ClearWhenTaskReset); // so if the app is relaunched, we don't show the display application.
StartActivity(i);
Then, I have my own content provider which does most of the work in OpenFile()
public override ParcelFileDescriptor OpenFile(Android.Net.Uri uri, string mode)
{
switch (sUriMatcher.Match(uri))
{
case FILE_ID:
if (mode != "r")
throw new Java.Lang.UnsupportedOperationException("Do not support write access: " + uri);
String id = uri.LastPathSegment;
Java.IO.File file = new Java.IO.File(Application.Context.CacheDir, id);
DownloadToFile(file, id);
return ParcelFileDescriptor.Open(file, ParcelFileMode.ReadOnly);
default:
throw new Java.Lang.IllegalArgumentException("Unknown Uri: " + uri);
}
}
It is not my original plan, but this way seems to work quite well and meets my needs.
hi is there a way to ask the os to open a file with its predefined programm?
I do not know what kind of file it might be, maybe a photo , txt anything.
I want to issue a command to the os to ask it to open the file(i have the filepath) with any program the os wants. It might be the case that the os opens the dialog asking the user to select one program to open the file(that is fine with me).
thanks
You do that via an intent chooser:
please refer to : http://developer.android.com/training/sharing/send.html
This is done through an Intent You will need to set the action and the data.
//from inside an Activity
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.fromFile(theFileYouMentioned));
try{
startActivity(i);
}
catch(ActivityNotFoundException) {
//user doesn't have an app that can open this type of file
}
I've a button on which I would like to open any file manager, and allow user to pick ONLY audio files. So i wrote something like this:
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select audio", SELECTED_AUDIO);
However, this doesn't work - it opens file manager (in my case Astro) and voalaaa - I can see and also pick any files I want - I mean, text files, video files, etc. Simple it doesn't filter or check anything...What am I doing wrong? Or its fault of Astro? I just want to show only audio files, or show all files but allow to pick only audio.
What am I doing wrong?
Nothing.
Or its fault of Astro?
Sort of. You assume that Astro knows the MIME type of all its files, and that it will actually use the MIME type to filter the results. There is nothing forcing Astro to do either of those things. The same holds true for any file manager or anything else that responds to ACTION_GET_CONTENT for audio/*. Now, it would be nice if Astro would do the filtering or something, but that's an issue you would have to take up with them.