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:
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
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:
I have image stored in the internal storage of the app. I can grab the path and I can succesfully set it to image view. But when I am trying to let the user open it using the gallery (intent), it displays black screen.
myIntent.setDataAndType(Uri.fromFile(file), mimetype);
intent = Intent.createChooser(myIntent, "Choose a viewer");
startActivity(intent);
I am pretty sure it has to do with permission that gallery cant access private storage of my app for some reason. But is there way to do that "beside moving the file to external storage"
Thanks
Use FileProvider to serve the file from internal storage. Quoting the documentation:
FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file:/// Uri.
A content URI allows you to grant read and write access using temporary access permissions. When you create an Intent containing a content URI, in order to send the content URI to a client app, you can also call Intent.setFlags() to add permissions. These permissions are available to the client app for as long as the stack for a receiving Activity is active.
Here is an easy 'single file based solution'
When ever you add a file, let Media Store Content Provider knows about it using
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(imageAdded)));
i have done some thing like this to show images from gallery.
private void pickFromGallery() {
Crop.pickImage(this);
}
<--->
crop is an android class.
this is a function in crop class.
public static void pickImage(Activity activity) {
Intent intent = (new Intent("android.intent.action.GET_CONTENT")).setType("image/*");
try {
activity.startActivityForResult(intent, 9162);
} catch (ActivityNotFoundException var3) {
Toast.makeText(activity, string.crop__pick_error, 0).show();
}
}
Through my application the user can choose a file from the filemanager and process the choosen file ( Example: A PDF document ) for other steps like printing or to email.
To do this I used the below code to display the intent, from which the user can choose a file from file-manager option.
protected void showFileChooser(String title, String type) {
Log.i(TAG, "FileChooserActivity showFileChooser(title,type)");
if (TextUtils.isEmpty(title)) title = getString(R.string.select_file);
if (TextUtils.isEmpty(type)) type = "*/*";
// Implicitly allow the user to select a particular kind of data
final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// Specify the MIME data type filter (Must be lower case)
intent.setType(type.toLowerCase());
// Only return URIs that can be opened with ContentResolver
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Display intent chooser
try {
startActivityForResult(
Intent.createChooser(intent, title),6384);
} catch (android.content.ActivityNotFoundException e) {
Log.i(TAG,"FileChooserActivity showFileChooser(title,type) Exception" +Log.getStackTraceString(e));
onFileError(e);
}
}
Problem in Android Kitkat version ( 4.4 ) :
Using the above code , I can access all files from the file manager ( i.e., both sub folder's and root folder) in android 4.3 and all android versions except Android 4.4.
In Android 4.4 , I can access files from the root folder, where as I cant access the files from the subfolders. I get java.io.FileNotFoundException as exception.
Note:
I installed Astro File Manager and through this I can access both root and sub-folders in Android 4.4. But I cant access the sub-folder files through Androids default file manager.
I have trying to open XLSX file in android using intent filter to select existing XLSX reader application but always getting file not found exception. The same code works fine for XLS file. I added WRITE_EXTERNAL_STORAGE permission in manifest and file path being correct only. File also available in SD Card. My tablet have sheet to go and OfficeSuite application to read XLSX files. Even i can able to open my XLSX file by clicking on it. But trying to open via my application gives problem. For XLSX file my code always goes to else part. Thanks in advance.
private void openSpreadSheet(String fileCompletePath) {
File file = new File(fileCompletePath);
if (file.exists()) {
try {
Log.d(TAG, "File exist");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/vnd.ms-excel");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(this,
"No application found to display spread sheet.",
Toast.LENGTH_SHORT).show();
}
} else {
Log.d(TAG, "File not exist");
}
}
If i remove if condition, then intent filter asked for choose application after choosing application, that selected application shows this error File not found(/Duplicate.xlsx:open failed:ENOENT(No such file or directory)).
Found Solution
Issue comes because I gave wrong file path.