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

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);

Related

Intent ACTION_GET_CONTENT returns uri even for deleted files

I am trying to read pdf files in the android app.
Fire following intent to get URI.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(intent, PDF_FILE_SELECTOR_INTENT_ID);
Problem is, the Downloads folder also shows old files that I have deleted.
Also, when I select those files a valid URI is returned in onActivityResult(). When I create File from URI and check exists() it returns false which makes sense as I have already deleted the file from the Downloads folder.
How can I make sure that the Downloads folder shown on ACTION_GET_CONTENT shows only files which are currently present and not deleted ones?
Thanks.
Instead of
intent.setType("application/pdf"); use the Intent.EXTRA_MIME_TYPES in the putExtra
In Java:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {
"application/pdf", // .pdf
});
startActivityForResult(intent, REQUEST_CODE);
In Kotlin
startActivityForResult(
Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
putExtra(Intent.EXTRA_MIME_TYPES, arrayOf(
"application/pdf", // .pdf
))
},
REQUEST_CODE
)
Update: Actual Download Folder & "Downloads" folder different in this case.
Download is for your actual downloaded files
Downloads is a history folder behaves like a short-cut and it's not cleared automatically when the actual file pointed to is manually removed. This might be most likely an expect behavior.
In your case , you need to hide the downloads folder ( still you can use Download). using this line of code will show "Internal storage" by default:
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
Call addCategory(Intent.CATEGORY_OPENABLE) to your Intent as recommended here: https://developer.android.com/reference/android/content/Intent#ACTION_GET_CONTENT

How can i do a menu chooser in android

I want to do a menu where u can choose the audio apps that are installed in your phone (like Youtube, Music, Skype...) so I donĀ“t know the exactly name of this type of menu (if this have a special name).
This should be what you are searching for: https://developer.android.com/training/basics/intents/sending.html
Intent intent = getPackageManager().getLaunchIntentForPackage("com.package.address");
if (intent != null) {
startActivity(launchIntent);
}
Replace com.package.address with the package name of the application you want to open.
If I understand, you would to send file to another app based on type of this file.
This code allows to send audio file to selected app.
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_SEND);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(Intent.createChooser(intent, "Choose app"));
More info

How can I ask the user to select the app he wants to use to open a file?

I'm trying to make my own file manager for android.
I am wondering how I could asks the user to choose the application he wants to use open a file. like when you are opening a .doc file and you have a bunch of .doc file readers. How would I be able to retrieve the list of apps that the user can use to read/open a file?
Try this code
Uri uri = Uri.parse("Your file name: //"+file.getAbsolutePath());
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
String type = "application/msword";
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent);
You should use implicit intent for your purpose. Below is the sample code for giving options to play a video file
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(Intent.createChooser(intent, "Complete action using"));
I hope it will help you out. For further details you may refer android official documentation as mentioned in below link
https://developer.android.com/training/basics/intents/sending.html#AppChooser
I think you can try this
Intent in= new Intent(Intent.ACTION_MAIN);
in.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
startActivity(in);

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);

Allow user to chose application for open file

In application user can download file and open it after download completed.
I can open window for choosing application something like this:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + path);
intent.setDataAndType(uri,"image/*");
context.startActivity(intent);
But here "image/*" is hard-coded. In my application user can download files with different extensions (txt, gif, png, doc, .etc).
Of course I can do something like this:
if(fileExt.equels("png") || fileExt.equels("jpg") || fileExt.equels("gif") )
intent.setDataAndType(uri,"image/*");
But then i need to write code for all extensions =/
So, can you suggest better way to filter this file extensions?
You can use activity chooser: (of course you need to provide MIME type as image/* so that only applications which can handle images will appear in the activity chooser.
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}

Categories

Resources