- Android 10
// Check if the app has permission to access external storage
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
// Create a file object representing a file in the Downloads directory
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "example.txt");
// Check if the file exists
if (file.exists()) {
// If the file exists, read its contents
try {
FileInputStream inputStream = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
String contents = stringBuilder.toString();
// Do something with the file contents
Log.d(TAG, "File contents: " + contents);
} catch (IOException e) {
e.printStackTrace();
}
} else {
// If the file does not exist, create it and write some content
try {
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write("Hello, world!".getBytes());
outputStream.close();
Log.d(TAG, "File created successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
// If the app does not have permission to access external storage, request it
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
}
In this example, we first check if the app has permission to access external storage using the checkSelfPermission() method of the ContextCompat class. If the app has permission, we create a File object representing a file named "example.txt" in the Downloads directory using the getExternalStoragePublicDirectory() method of the Environment class. We then check if the file exists using the exists() method of the File class. If the file exists, we read its contents using a FileInputStream and a BufferedReader. If the file does not exist, we create it using a FileOutputStream and write the string "Hello, world!" to it. If the app does not have permission to access external storage, we request it using the requestPermissions() method of the ActivityCompat class.
- Android 13+
// Create a file object representing a file in the app's internal storage directory
File file = new File(getContext().getFilesDir(), "example.txt");
// Check if the file exists
if (file.exists()) {
// If the file exists, read its contents
try {
FileInputStream inputStream = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
String contents = stringBuilder.toString();
// Do something with the file contents
Log.d(TAG, "File contents: " + contents);
} catch (IOException e) {
e.printStackTrace();
}
} else {
// If the file does not exist, create it and write some content
try {
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write("Hello, world!".getBytes());
outputStream.close();
Log.d(TAG, "File created successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
In this example, we create a File object representing a file named "example.txt" in the app's internal storage directory using the getContext().getFilesDir() method. We then check if the file exists using the exists() method of the File class. If the file exists, we read its contents using a FileInputStream and a BufferedReader. If the file does not exist, we create it using a FileOutputStream and write the string "Hello, world!" to it.
- In Android 10 (API level 29) and lower, you can use ContextCompat.checkSelfPermission() to check if your app has a permission. However, starting from Android 11 (API level 30), the checkSelfPermission() method has been deprecated in favor of Environment.isExternalStorageManager(), which checks if your app has been granted the "All files access" permission.
- In Android 12 (API level 31) and higher, the Environment.isExternalStorageManager() method is the only way to check if your app has been granted the "All files access" permission. Additionally, starting from Android 11, the WRITE_EXTERNAL_STORAGE permission has been deprecated and replaced with more specific permissions like MANAGE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE.
- in Android 13 (which I assume you mean Android 12 since there is no Android 13 yet), you can no longer use ContextCompat.checkSelfPermission() to check for the WRITE_EXTERNAL_STORAGE permission. Instead, you should use Environment.isExternalStorageManager() to check if your app has been granted the "All files access" permission, or use the more specific permission like MANAGE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE.
கருத்துரையிடுக