I am using Room DB to save data locally in android.
Now, I want to preserve the data even user uninstall the application or do clear storage.
So, I created Room DB in following location.
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath()
Everything works fine.
But if I clear the storage or uninstall the application and I open the application again, I am getting following error.
Caused by: java.util.concurrent.ExecutionException: android.database.sqlite.SQLiteCantOpenDatabaseException: Cannot open database '/storage/emulated/0/Download/database/hhcf': File /storage/emulated/0/Download/database/hhcf is not readable
This issue coming in Android 11. For Android 9 no excetion is there.
I have granted following permissions as well.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
And,
android:requestLegacyExternalStorage="true"
in application tag.
Any help would be appreciated.
You need to request user permission to get this permission, adding it only in manifest won't work, at least it didn't for me. I did something like this:
#RequiresApi(api = Build.VERSION_CODES.R)
public void checkFileAccessPermission() {
if (Environment.isExternalStorageManager()) {
return;
}
Intent getPermission = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(getPermission, 123);
}
It's a solution from this thread: How to obtain MANAGE_EXTERNAL_STORAGE permission
But remember that if you want your app to be on the google play store you need to justify to google why you need this permission like this article says: https://support.google.com/googleplay/android-developer/answer/10467955?hl=en
If you use another path for files e.g method getFilesDir() you won't have this problem either, but it's only good if developing a new app, or something like that.
Related
I uploaded an android photo compress application in play store and I checked every thing is ok, but some users have issue that the application can not save compressed photos, I checked that and I found this error
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FileSystemException: Cannot create file, path = '/storage/emulated/0/My Folder/photo.jpg' (OS Error: Operation not permitted, errno = 1)
This is permissions AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>
<uses-permission android:name="android.permission.CAMERA"/>
This line inside application tag
android:requestLegacyExternalStorage="true"
I looked for some solutions and founded this permission line
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
But when ask for this permission android shows to user warning the application will access to all files without asking permission, I feel this is worrying for users and some may be afraid of this permission, however I uploaded the application but it was rejected because using MANAGE_EXTERNAL_STORAGE permission.
you are right regarding MANAGE_EXTERNAL_STORAGE permission. Your app will be removed from play store if you don't have valid reasons. You may have to create a video and explain why you need these permission, and how scoped storage can't help you.
Now, you should know one thing that you are trying to create a folder directly inside internal storage. This will work in lower android versions but will fail in Android 10 or above even when you have WRITE_EXTERNAL_STORAGE granted.
Try creating your files inside public directories like Download or Documents. If the files downloaded doesn't need to be accessed by users then you can always save in your package folder context.getExternalFilesDir(null)
Hope this helped you! If you have any further doubts feel free to comment down below.
I'm trying to achieve some clean up tools. More and more manufacturers have forbidden rooting devices due to some "security reason", it's forbidden NOT to request for unlock.
After API 28, This code will make error:
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, 1); // Request permission or not, Will got same result
File rootFolder = Environment.getExternalStorageDirectory(); // That is working fine
rootFolder.listFiles(); // That will return null
Sure, I can use this:
android:requestLegacyExternalStorage="true"
But I belive that will be killed in future.
So, Any elegant way to manage SDCard?
On Android 10 Environment.getExternalStorageDirectory() and Environment.getExternalStoragePublicDirectory() will return storage paths but paths are not readable or writable.
For Android 10 you can continue to use paths provided by Environment.getExternalStorageDirectory() and Environment.getExternalStoragePublicDirectory() if you add android:requestLegacyExternalStorage="true" to application tag in manifest file. At runtime your app can call Environment.isExternalStorageLegacy() to check if the request has been done.
Another (not known) possibility (only for Android 10) is to add <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> to manifest file.
The user has to go to the advanced settings of the app and enable from Advanced settings Install unknown apps | Allow from this source.
The nice thing with this is that the user can switch the access rights. You can make it easier for the user if you implement an intent for
Settings.ACTION_APPLICATION_DETAILS_SETTINGS where he can change the settings.
A funny thing is that Environment.isExternalStorageLegacy() returns true then too.
Compiling for Android 11 both options do not work on an Android 11 device. (But they continue to work for Android 10 devices). The paths of Environment.getExternalStorageDirectory() and Environment.getExternalStoragePublicDirectory() are usable again in read mode and very often in write mode too. And this is great as one can simply list the contents of directories like Download or Pictures or DCIM/Camera again using the File class.
But adding <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> to manifest file and implementing an intent for
Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION will give your app read/write access for all files even on removable micro sd card.
(Finally you can remove the google ban not being able to read/write your own micro sd card on your own Android device using your own app).
Environment.isExternalStorageManager() can be used to check if the permission is on/off.
As long as you do not try to upload your app to the play store you are fine.
use android:requestLegacyExternalStorage="true" in your Manifest below <application
I'm testing my app on an emulator. I have an export function where I create and write to a file in the external storage's downloads directory. And I also have an import function where I read a file from the external storage's downloads directory.
From Android documentation:
If the device is running Android 5.1 or lower, or your app's target SDK is 22 or lower: If you list a dangerous permission in your manifest, the user has to grant the permission when they install the app; if they do not grant the permission, the system does not install the app at all.
If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.
My emulator is running on Android 6.0 and my app's target SDK is 25, therefore I must also request each dangerous permission it needs while the app is running. I did so for the export functionality and everything works properly. However, when I'm implementing the import function I didn't request a permission during runtime. And the strange thing is I'm still able to read from my external storage's permission without READ_EXTERNAL_STORAGE being requested and granted at runtime. READ_EXTERNAL_STORAGE is a dangerous permission according to this Android documentation .
To verify, I made sure to disable permissions before I started using the feature and after it is completed, I verified again that the permission still wasn't granted. Although I'm happy with the behaviour since it's working without me requesting permission at runtime, but according to the documentations I don't believe this behaviour is expected. That's why I will like to know what's causing this and to figure out the problem before I publish any changes for the app.
Here's a code snippet of my manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The code snippet where I pick a file to read:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/*");
startActivityForResult(intent, GET_FILE_RESULT_CODE);
The code snippet where I read the file chosen from the code snippet above (exportFile is simply the URI from onActivityResult):
BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(context.getContentResolver().openInputStream(exportFile)));
String line;
// Skip first header line
br.readLine();
while ((line = br.readLine()) != null) {...}
Thanks!
There's a well explanation here,
READ_EXTERNAL_STORAGE
Provides protected read access to external storage. In Android 4.1 by
default all applications still have read access. This will be changed
in a future release to require that applications explicitly request
read access using this permission. If your application already
requests write access, it will automatically get read access as well.
There is a new developer option to turn on read access restriction,
for developers to test their applications against how Android will
behave in the future.
In short, READ_EXTERNAL_STORAGE only exists as of Jelly Bean (Level 16). So, unless you're using a Jelly Bean phone and set the developer option "Protect USB storage" it won't be a problem.
You know,Android Runtime Permissions are grouped, since you applied for WRITE_EXTERNAL_STORAGE permission in the manifest already, so there's no need to apply for READ_EXTERNAL_STORAGE permissions.Both of them are the same group.
I have and AIR application that have been working almost 2 years. In the beginning of the app, I'm checking is there a folder in documents directory for storing images. That was working just fine since last update.
in app.xml I have:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Folder is regularly created...
public class FileManager {
public static var MainFolderName:String = "FolderNameToCreate";
public static function managerInit():void {
var mainFile:File = File.documentsDirectory;
mainFile = mainFile.resolvePath(MainFolderName);
if (!mainFile.exists) {
mainFile.createDirectory();
}
.......
}
....
}
If I omit this, application is working, mean can be initialized, if not (new instals) cannot open, app crashes.
After one of crashes I have open permission settings for app and see that Storage permission is off? I enable it, and after that app works. I have tested it on Note 4, with Android version 6.0.1.
Is there any additional authorization for using external storage?
I have created an android application which downloads images from URLs given and download and save it into my sdcard. The image is to be saved into /mnt/sdcard/TempImages folder. and the image is then loaded into imageview for viewing it in my android activity. Till, today morning the application was working very fine, but from today morning onward, when I try to run the application, the image cannot be viewed in my activity. I have been loading the images from my own web server and I have given full permission for read or write access to the files in the server. So there is no problem from the server side. The problem is from the client side. This is the error log while trying to run the application. How should I add permission for my application in the /mnt/sdcard/TempImages folder for read and write access.
W/System.err(458): java.io.FileNotFoundException: /mnt/sdcard/TempImages/-1267781495 (Permission denied)
Make sure your app has the proper permissions to be allowed to write to external storage:
It should look something like this in your manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
add WRITE_EXTERNAL_STORAGE permission in manifest
Make sure you add the correct permission on your app manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />