I need to create files and read/write to external storage and the files should persist after uninstallation of the application. It should work on Android 12.
import android.content.Context;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileUtil {
public static void writeFileToExternalStorage(Context context, String fileName, String content) {
File file = new File(context.getExternalFilesDir(null), fileName);
try {
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(content.getBytes());
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String fileName = "my_file.txt";
String content = "Hello";
FileUtil.writeFileToExternalStorage(getApplicationContext(), fileName, content);
File my_file.txt
will be deleted after uninstallation of the app. I need it to persist.
I will be writing text files and images related to it (e.g. one subdirectory of the app directory would contain files f1.txt, img1.jpg, f2.txt, img2.jpg ... fn.txt, imgn.jpg).
I don't want to use database or shared preferences for it.
How can I do it?
In this video there is said, that everytime the app reads/writes to a file, the user has to go through dialog, but this is what I don't want. It's very uncomfortable for the user.