I have written a code that open a file like pdf. For that I used intent like the below one:
public void openFile(Context context, File file) {
String typeFile = "application/pdf";
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), typeFile);
PackageManager pm = context.getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType(typeFile);
Intent openInChooser = Intent.createChooser(intent, "Choose");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
if (resInfo.size() > 0) {
try {
context.startActivity(openInChooser);
} catch (Throwable throwable) {
Toast.makeText(context, "No application found which can open the file", Toast.LENGTH_SHORT).show();
// PDF apps are not installed
}
} else {
Toast.makeText(context, "No application found which can open the file", Toast.LENGTH_SHORT).show();
}
}
}
My problem is when I run this code on Android 10 and 11, the Toast in else is executed but in older versions these codes work correctly.
So what should I do to open file in Android 10 and 11?
Your app should be crashing on Android 7.0+ with a FileUriExposedException. You need to get rid of Uri.fromFile() and switch to FileProvider.
Also, you can get rid of queryIntentActivities(). You do not need it, partly because there should always be a chooser, and partly because you have the try/catch block to catch the ActivityNotFoundException if there is no chooser for some strange reason. If your reason for queryIntentActivities() is to avoid an empty chooser, stop creating a chooser — the OS will add one automatically if one is appropriate. Alternatively, for Android 11+, you will need to deal with package visibility rules to be able to call queryIntentActivities(). See this and this for more.
In my Android app the user can add files locally and retrieve files from the server. They are displayed in a list in my app. I want the user to be able to select a file, and display the given file in a file explorer (analagous to "Show in folder" after downloading something in Chrome). Is this possible? I haven't seen a way to do it!
Thanks
P.S. this is what I've tried so far - hacked together from various sources.
string path = TSetupBase.Dirs.AttachmentsFolder_ + attachment.FileName;
Android.Net.Uri selectedUri = Android.Net.Uri.Parse(path);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(selectedUri, "resource/folder");
StartActivity(intent);
I get an exception saying it was unable to find an app to handle this intent.
Try this:
Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
fileintent.setType("gagt/sdf");
try {
startActivityForResult(fileintent, PICKFILE_RESULT_CODE);
} catch (ActivityNotFoundException e) {
Log.e("tag", "Problems XXXXX ");
}
** EDIT **
After your comment that you want the file manager to open with the path on which the file chosen is present then you have to pass the file path as well.. Check the edited code below, line #2
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/"+the_path_of_your_file);
startActivityForResult(intent, YOUR_RESULT_CODE);
I'm sure this should work for you.
I'm using the following code to allow the user to access My Files from a Samsung android device. I'm trying to open certain path in the My Files when the user press the button. Right now it will open the default path. Can you tell me if it is possible to pass the path through when open My Files from Android? Below is the code that I use. Can you give me the code that will open My Files into certain Path? Thank you in advance for your help.
public void AccessingFiles(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
// special intent for Samsung file manager
Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
// if you want any file type, you can skip next line
sIntent.putExtra("CONTENT_TYPE", "*/*");
sIntent.addCategory(Intent.CATEGORY_DEFAULT);
Intent chooserIntent;
if (getPackageManager().resolveActivity(sIntent, 0) != null){
// it is device with samsung file manager
chooserIntent = Intent.createChooser(sIntent, "Open file");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});
}
else {
chooserIntent = Intent.createChooser(intent, "Open file");
}
try {
startActivityForResult(chooserIntent, CHOOSE_FILE_REQUESTCODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
}
}
For anyone looking for the solution to the same question this is how I open My Files pointed at a certain path:
File folder = new File(""); // construct the path
Intent fmIntent = new Intent(Intent.ACTION_MAIN);
fmIntent.setComponent(new ComponentName("com.sec.android.app.myfiles", "com.sec.android.app.myfiles.MainActivity"));
fmIntent.setData(Uri.parse(folder.toString()));
To be noted that fmIntent.setData(Uri.fromFile(folder)) did not work for me.
The solution is tested with My Files 1.19.0 and this is how I came to it:
I created a custom Launcher;
My Files can create home screen shortcuts to a specific path;
When the install shortcut intent was sent to my launcher I dumped its contents so that I can use them to create my fmIntent from the above code snippet.
I'm trying to import a file into my application, below is the code :
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath());
intent.setDataAndType(uri, "*/*");
startActivityForResult(Intent.createChooser(intent, "Select File"),
REQUESTCODE);
What i want is that before firing the intent i want to programatically check whether any File explorer is present on my device or not? If yes then fire the intent but if no then i want to redirect user to Play store to download one, as it is mandatory for selecting a file. Is this feature possible to implement? Kindly enlighten me on this. Thanks in advance.
You can check if the file explorer is present or not using the below code, If not then ask user to download the app.
PackageManager packageManager = getActivity().getPackageManager();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
PackageManager.GET_ACTIVITIES));
if (list.size > 0) {
// File explore is present (Size tells how many file explorers are present)
} else {
// Not present
// Just pointing to this app - https://play.google.com/store/apps/details?id=com.rhmsoft.fm
// You can choose whichever you need
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.rhmsoft.fm")));
} catch (android.content.ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.rhmsoft.fm")));
}
}
I would like to start an intentchooser for apps which can return any kind of file
Currently I use (which I copied from the Android email source code for file attachment)
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Intent i = Intent.createChooser(intent, "File");
startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);
But it only shows "Gallery" and "Music player" on my Galaxy S2. There is a file explorer on this device and I would like it to appear in the list. I would also like the camera app to show in the list, so that the user can shoot a picture and send it through my app.
If I install Astro file manager it will respond to that intent, too. My customers are Galaxy SII owners only and I don't want to force them to install Astro file manager given that they already have a basic but sufficient file manager.
Any idea of how I could achieve this ? I am pretty sure that I already saw the default file manager appear in such a menu to pick a file, but I can't remember in which app.
Not for camera but for other files..
In my device I have ES File Explorer installed and This simply thing works in my case..
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
Samsung file explorer needs not only custom action (com.sec.android.app.myfiles.PICK_DATA),
but also category part (Intent.CATEGORY_DEFAULT) and mime-type should be passed as extra.
Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
You can also use this action for opening multiple files: com.sec.android.app.myfiles.PICK_DATA_MULTIPLE
Anyway here is my solution which works on Samsung and other devices:
public void openFile(String mimeType) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(mimeType);
intent.addCategory(Intent.CATEGORY_OPENABLE);
// special intent for Samsung file manager
Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
// if you want any file type, you can skip next line
sIntent.putExtra("CONTENT_TYPE", mimeType);
sIntent.addCategory(Intent.CATEGORY_DEFAULT);
Intent chooserIntent;
if (getPackageManager().resolveActivity(sIntent, 0) != null){
// it is device with Samsung file manager
chooserIntent = Intent.createChooser(sIntent, "Open file");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});
} else {
chooserIntent = Intent.createChooser(intent, "Open file");
}
try {
startActivityForResult(chooserIntent, CHOOSE_FILE_REQUESTCODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
}
}
This solution works well for me, and maybe will be useful for someone else.
this work for me on galaxy note
its show
contacts,
file managers installed on device,
gallery,
music player
private void openFile(Int CODE) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
startActivityForResult(intent, CODE);
}
here get path in onActivityResult of activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String Fpath = data.getDataString();
// do somthing...
super.onActivityResult(requestCode, resultCode, data);
}
This gives me the best result:
Intent intent;
if (android.os.Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
} else {
String[] mimeTypes =
{"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
"application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
"text/plain",
"application/pdf",
"application/zip", "application/vnd.android.package-archive"};
intent = new Intent(Intent.ACTION_GET_CONTENT); // or ACTION_OPEN_DOCUMENT
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
}
Turns out the Samsung file explorer uses a custom action. This is why I could see the Samsung file explorer when looking for a file from the samsung apps, but not from mine.
The action is "com.sec.android.app.myfiles.PICK_DATA"
I created a custom Activity Picker which displays activities filtering both intents.
If you want to know this, it exists an open source library called aFileDialog that it is an small and easy to use which provides a file picker.
The difference with another file chooser's libraries for Android is that aFileDialog gives you the option to open the file chooser as a Dialog and as an Activity.
It also lets you to select folders, create files, filter files using regular expressions and show confirmation dialogs.
The other answers are not incorrect. However, now there are more options for opening files. For example, if you want the app to have long term, permanent acess to a file, you can use ACTION_OPEN_DOCUMENT instead. Refer to the official documentation: Open files using storage access framework. Also refer to this answer.