Open folder on card - android

I thought this was something really simple, but it's giving me more work than the rest of the whole app.
I just want a button to open the "Downloaded Files" folder of my app. That's all. No file picker, nothing complicated. All the button has to do is open a folder with the default app (or open the app chooser if there's no default).
I tried different solutions I saw here on StackOverflow, but nothing seems to work for me.
Example 1:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/MyDownloadedFiles/");
intent.setDataAndType(uri, "text/plain");
startActivity(Intent.createChooser(intent, getString(R.string.action_downloaded)));
On my Android 4.4, this opens the KitKat file picker, and doesn't even open in the folder I want... it seems to be defaulting to the card root. But I'm sure the folder exists and the path being given to the Intent is indeed correct.
Example 2:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + "/MyDownloadedFiles/");
intent.setDataAndType(uri, "text/plain");
startActivity(Intent.createChooser(intent, getString(R.string.action_downloaded)));
This one opens some blank activity with a popup saying: "Error occurred when getting file Content: MyDownloadedFiles".
How can I make my app to simply have a shortcut to a folder where it puts contents into?

wow.. Finally!! After many things I tried, the only way it worked was by wrapping the URI string into a File first, and then do a Uri.fromFile:
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/MyDownloadedFiles/");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "*/*");
startActivity(Intent.createChooser(intent, getString(R.string.action_downloaded)));

I think this one could work for you:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/yourFolder/");
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, "Open /sdcard/yourFolder"));

Related

Open Folder Using Default File Explorer

I am trying to open "MyFolder" but it is opening recent files folder.
I have tried many techniques, nothing seems to work.
All are similar to this code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() +
File.separator + "MyFolder" + File.separator);
intent.setDataAndType(uri, "*/*");
startActivity(intent);
Is it possible, if yes, how can I do it?
Any help would be appreciated.
First, there is no standard Intent action to "open" a filesystem directory on Android.
Second, the value that you pass to Uri.parse() needs to be a string form of a Uri, with a scheme. If you are going to create a Uri from a File, use Uri.fromFile(), rather than turning the File into a String and passing that String to Uri.parse().
Third, ACTION_GET_CONTENT does not take a Uri.

Android intent doesn't work on .apk files

I'm developing a File Manager, and I open files this way:
fileName = fileToOpen.getName();
fileName = fileName.substring(fileName.lastIndexOf('.') + 1);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".provider", fileToOpen);
intent.setDataAndType(uri, MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileName));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
This can handle almost all extension, but when I click on .apk files an ActivityNotFoundException gets thrown. I would like to launch the android installation screen.
Only on Android 7.0+ will the package installer know how to work with content Uri values. For earlier versions of Android, you have no choice but to have the APK on external storage somewhere and use a file Uri.

How to create an intent to open .doc file using Polaris Viewer on Android?

I am trying to open a .doc file from my SD card using Polaris Viewer.
I keep on getting a message saying "This document cannot be opened".
weird thing is that I CAN open it from elsewhere.
I have ES File Explorer on my phone and I can open through there. It does it via Polaris Viewer so the file is obviously okay.
The only thing I can think of is that I have a problem with my intent.
Is there any way to see exactly what intent ES File Explorer sent?
This is my code (textOpenUri is a full path name of the file to be opened):
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(textOpenUri, "application/msword");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
I did a small test (just to make sure it is not defaulting to some other app) using:
List<ResolveInfo> list = getActivity().getPackageManager().queryIntentActivities(intent,0);
and I get back Polaris as the only app that can deal with the intent.
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/msword");
this.startActivity(intent);
startActivity(intent);

Force browser chooser when opening a local html file in Android

Maybe this question or similar has been asked before, but I am really struggling for days now and couldn't find a precise answer and solution.. I have a local html file that I want to open with a browser chooser. So far I have this code:
File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "index/index.htm");
Uri webPageUri = Uri.fromFile(file);
browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setDataAndType(webPageUri, "text/html");
chooserIntent = Intent.createChooser(browserIntent, "Choose your app:");
if (browserIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooserIntent);
}
which mostly gives me the html viewer and file/text editor as options. Additionally to this options, on some devices one browser option appears (for example opera).
How to tell the chooser that my file is an html file, and force the browser chooser?
Please share...
Where's chooserIntent generated?
Replace
startActivity(whateverIntent)
with
startActivity(Intent.createChooser(browserIntent, "Open webpage using:"));

How to open the My Files folder in Android Programatically (using Intent)?

I am using the below code which opens up the Gallery, Music Player, Dropbox and Contacts, i want the My Files folder to get open programatically, please let me know if there are any specific intent parameters i need to pass to get the File Manager open.
if it is not possible through intent then please give me a snippet or an hint to open the My Files folder programatically.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
Intent i = Intent.createChooser(intent, "View Default File Manager");
startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);
Thanks.
You can use this code to file the files.
int PICKFILE_RESULT_CODE=1;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
this will help you to browse the files from your storage.
Its best that you include a library in your project which handles this scenario.
This worked for me:
This library shows the list of third-party apps. It also has its own file browser for selecting files.
Bad thing is, most Android distributions may or may not ship with a file manager, and even so, may be not with the one which handles CHOOSE_FILE_REQUESTCODE.
So, you are left to create your own file picker activity. Luckily there are many ready made ones available:
http://code.google.com/p/android-filechooser/
https://developers.inkfilepicker.com/docs/android/
If you want to open samsung My Files application try this below code.
Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
startActivityForResult(intent, CHOOSE_FILE_REQUESTCODE);
You have to specifically mention the package name of the explorer application. Please find the example below to open a specific folder in ES Explorer.
public void openfolderInexplorer(String path){
Intent intent = this.getPackageManager().getLaunchIntentForPackage("com.estrongs.android.pop");
if (intent != null) {
// If the application is avilable
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.parse(path);
intent.setDataAndType(uri, "resource/folder");
this.startActivity(intent);
} else {
// Play store to install app
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" +
"com.estrongs.android.pop"));
this.startActivity(intent);
}
try this below code. if any file manager available , then it will pop up in a form of menu to choose appropriate for the user.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, CHOOSE_FILE_REQUESTCODE);

Categories

Resources