Use Android to support library document file framework to implement file introduction and export function
Use Android to support library document file framework to implement file introduction and export function
In Android applications, the introduction and export function of files are very common needs.Android provides a strong support document framework (SUPPORT DOCUMENT FILE FREAMEWORK), which can help us handle the reading and writing and management of files.This article will introduce how to use this framework to implement the file import and export function.
1. Import file
To implement the file import function, we need to use Android file selectioner to select the file to be imported.First, add the following dependencies to your Android project to introduce the required support library file framework:
implementation 'androidx.documentfile:documentfile:1.0.1'
implementation 'androidx.appcompat:appcompat:1.3.1'
Next, create a button or other view to trigger the opening of the file selector:
Button importButton = findViewById(R.id.import_button);
importButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, IMPORT_REQUEST_CODE);
}
});
In the above code, we created a intention to open the document and specify the Action_open_document.Set the file type to be imported to any type (settype ("*/*") and specify the request code of the return result.
Finally, rewrite the onActivityResult method in Activity to process the returned results:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMPORT_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
Uri uri = data.getData();
// Use URI to read and process the imported files
// ...
}
}
In the above code, we first check whether the request code and the result code match the import request, and then obtain the URI of the imported file from the returned Intent. You can use the URI to read and process the imported file.
2. Export file
To implement the file export function, we need to obtain the URI of the file to be exported and pass it to the selected target application.Here, we will export files in a file.
First, create a file to be exported in the application, and pass its URI to the file export function:
File exportfile = new file (getfilesdir (), "exported_file.txt"); // Create the file to be exported to the file to be exported
Uri Exporturi = FileProvider.geturifileFile (this, "com.yourPackage.fileProvider", exportfile); //
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Intent.putextra (INTENT.EXTRA_TITLE, "Exported_file.txt"); // Set the initial name of the export file
Intent.putextra (Intent.extra_stream, Exporturi); // pass the URI of the exported file to the intent
StartActivityForresult (INTENT, Export_request_Code); // Start file export the selector selectioner
In the above code, we first create a file to be exported and obtain the URI of the file (we use FileProvider to generate URI for other applications).Then, we created a new INTENT and specified the type and name of the exported file.Finally, we attached the URI of the exported file as an additional data to the INTENT and start the file exporter.
Next, rewrite the onActivityResult method in Activity to handle the returned results:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == EXPORT_REQUEST_CODE && resultCode == RESULT_OK) {
// Here you can perform some follow -up operations that export files are processed
// ...
}
}
In the above code, we first check whether the request code and result code match with the export request, and then perform some subsequent processing operations to export files in this method.
The above is the steps and example code of the introduction and export function of the file with Android to support the file framework of the library document.By using this framework, you can easily handle the read and write and manage the files, and provide a better user experience.Hope this article will help you!