When I need to open a file with SAF (from storage or from DocumentsProvider) I just call Intent :
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(TYPE_ALL);
try {
startActivityForResult(intent, OPEN_REQUEST_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, R.string.excaption_not_found_open,
Toast.LENGTH_SHORT).show();
}
When I need to delete a FILE I do just the same, except OPEN_REQUEST_CODE - I change it with DELETE_REQUEST_CODE, get URI and call Delete() method.
The problem is I don't know how to delete DIRECTORY. I have a method that does it pragmatically, but I can't figure how to use it with standard file viewer - it only OPENS directory - there is no Intent to delete it...
I guess I have to write my own File Manager to do that. Is there any other way?
Check this from the Android Developer documentation:
Related
I need to open a specific folder in Android 10, so that only the files inside that folder can be seen in my app.
I have tried this:
try {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
File f = new File(getExternalFilesDir(null) + "/MyFolder");
Uri uri = FileProvider.getUriForFile(getApplicationContext(),getApplicationContext().getPackageName()+".provider",f);
Toast.makeText(MainActivity.this,f.getPath(),Toast.LENGTH_LONG).show();
intent.setDataAndType(uri,"text/plain");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
catch (Exception e) {
Log.d("Exception",e.toString());
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
But, it opens the entire tree.
What I actually want is to open that folder. The files in the folder should be openable by other apps. Any help will be appreciated.
I need to open a specific folder in Android 10, so that only the files inside that folder can be seen in my app
Sorry, but that is not an option.
The closest thing to what you want is EXTRA_INITIAL_URI. However:
It takes a document or tree Uri that you obtained previously from the Storage Access Framework, not a FileProvider Uri; and
It does not prevent users from navigating elsewhere — it just controls the starting point
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
So the problem I got is, that I want the user to pick an xml file using ACTION_GET_CONTENT, which configures the application, but after a restart of the application I don't have the URI permission anymore and it crashes.
I could use ACTION_OPEN_DOCUMENT and persist the URI permission. But with ACTION_OPEN_DOCUMENT the file that I want to access is no longer shown in the file picker.
This is ridiculous, I just want to open a file, that the user picked himself! Picking the xml file after every restart of the app is not an option.
public void onChooseFileButtonClicked(View view) {
Intent fileIntentSicherung;
fileIntentSicherung = new Intent(Intent.ACTION_GET_CONTENT);
fileIntentSicherung.addCategory(Intent.CATEGORY_OPENABLE);
//fileIntentSicherung.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
//fileIntentSicherung.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
fileIntentSicherung.setType("text/xml");
try {
startActivityForResult(fileIntentSicherung, CHOOSE_FILE_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
The file will be stored in the root directory, that is shown when connecting via usb to a pc.
With ACTION_GET_CONTENT:
With ACTION_OPEN_DOCUMENT:
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.
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);