but after Intent.ACTION_CREATE_DOCUMENT file has created in user defined dir, but OS throws error and that file didn't write data (i.e. file has size of 0 bytes). Why it didn't work? And what function i need to use?
Assistant
User
from what class ParcelFileDescriptor to import?
Assistant
ParcelFileDescriptor is a class from the Android framework, used to gain access to the contents of a file through a file descriptor. It can be used to read or write data from streams, such as when using to interact with URI data.
The issue you're encountering with the Intent.ACTION_CREATE_DOCUMENT and file writing likely stems from incorrect usage or misunderstanding of Android's storage permissions and output file handling, specifically when dealing with user-specified directories outside of the app's private storage.
If you're using Intent.ACTION_CREATE_DOCUMENT, the user chooses a location via the system's file picker UI to create a file. After the file is created, Android returns a URI that your app can use to write to it. This process involves handling URIs and Content Resolvers, instead of traditional File objects as in your code.
Let's revise the method to handle files using Intent.ACTION_CREATE_DOCUMENT correctly:
Start the file chooser activity:
Create an intent to let the user choose the file type and location.
Handle the file URI:
Use the returned URI with a ContentResolver to obtain an OutputStream.
Copy data from the internal file:
Read from the existing internal file and write to output stream obtained from URI.
Here is a revised approach incorporating these steps:
Step 1: Launch the Document Creator Intent
You can trigger this via a method in your activity to begin the file creation process.
java
public void createFile() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/json"); // MIME type of the file
intent.putExtra(Intent.EXTRA_TITLE, "exportedfile.json");
startActivityForResult(intent, WRITE_REQUEST_CODE);
}
Step 2: Handle the activity result
Override onActivityResult in your activity to handle the file creation:
java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == WRITE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri uri = data.getData();
try {
exportFile(this, "internalFileName.json", uri);
} catch (IOException e) {
e.printStackTrace();
// Handle error
}
}
}
}
Step 3: Write to the selected URI
Modify your exportFile method to work with Uri and use a ContentResolver:
With these modifications, you now properly handle file output using Android's Storage Access Framework (Intent.ACTION_CREATE_DOCUMENT). This adheres to Android's recommended practices for file sharing and storage management, especially with the stricter enforcement of storage permissions in recent Android versions (Android 10+ Scoped Storage).
ContentResolver
To use ParcelFileDescriptor in your Android application, make sure to import it from the Android SDK libraries:
java
import android.os.ParcelFileDescriptor;
This import comes from the android.os package, which contains core operating system services and types in Android. You don’t need any additional libraries other than the standard Android SDK.
Here's a brief usage in context to reiterate its usage with ContentResolver when writing to a file via a Uri that a user selected with Intent.ACTION_CREATE_DOCUMENT:
In this code, ParcelFileDescriptor pfd is obtained from a ContentResolver using openFileDescriptor(uri, "w"). Here "w" indicates that the file should be opened for write access. The file descriptor is then used to create a FileOutputStream, which in turn is used to write to the file represented by the URI. After you're done with the file descriptor, it should be closed to release resources, which is managed by the try-with-resources statement in Java, that automatically closes resources after use.
Make sure your app has the necessary permissions declared and requests them appropriately at runtime if you are targeting Android 6.0 (API level 23) or higher. These permissions include READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE for accessing files outside your app's private storage. Starting with Android 10 (API level 29), consider using the scoped storage model to access files only in app-specific directories or through the Storage Access Framework as shown in your use case.