Android 11 Permission Issue - android

my flutter code is
var storageInfo = await PathProviderEx.getStorageInfo();
var root = storageInfo[0]
.rootDir;
var fm = FileManager(root: Directory(root));
await fm.filesTree(
excludedPaths: [
'/storage/emulated/0/Android',
],
extensions: ['pdf']
);
error occur
E/flutter (16591): FileSystemException: Directory listing failed, path = '/storage/emulated/0/Android/obb' (OS Error: Permission denied, errno = 13)
E/flutter (16591): #0 FileManager.dirsTree (package:flutter_file_manager/src/file_manager.dart:110:7)
E/flutter (16591): #1 FileManager.filesTree (package:flutter_file_manager/src/file_manager.dart:132:34)
I have one issue to get list of files from stroage in android 11 from Flutter app
i already add both permission in AndroidManifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

We need to request manage_external_storage permission via permission_handler.
Add this permission to AndroidManifest.xml
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
Request permission as shown below
await Permission.manageExternalStorage.request();
Then we can read any file and write to any location in Android 11.

The right solution will be to add android:requestLegacyExternalStorage="true" to your AndroidManifest file and in build.gradle change targetSdkVersion 29 whereas keeping compileSdkVersion 30. This should work fine.

Related

Storage Permission in Android 13 - Flutter

I'm making the use of read and write permission for accessing external storage and for getting the permission, I'm using the permission handler package
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
For getting the permission from user
var status = await Permission.storage.request();
if(status.isGranted){
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => const Home()));
} else if(status.isPermanentlyDenied){
openAppSettings();
}
The above permissions and code working perfectly for android versions till 12 but when it comes to android version 13, it is not working up, it just opens up the app setting instead of asking the permission

Android 10: Delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER

Target SDK in my project is 31 and when I'm trying to delete a file from DCIM directory, I'm getting following error such as delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER.
Please consider file path like this: "/storage/emulated/0/DCIM/Screenshots/pic1.jpg"
I have tried so much to find the result every where including stack overflow, but didn't succeeded yet. Even I changed target SDK to lower 30 but not worked.
Following are the solutions that I had already worked on but nothing works:
1.flags in manifest file
android:requestLegacyExternalStorage="true"
android:preserveLegacyExternalStorage="true"
2.permissions in manifest file
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="40"
tools:replace="android:maxSdkVersion" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
Please find the attached code
val fileToDelete = File("/storage/emulated/0/DCIM/Screenshots/pic1.jpg")
fileToDelete.delete()
NOTE: filepath is hardcoded only to explain better
Please find the attached log
2022-03-21 11:22:35.331 8639-20226/com.filepickerdemo D/ContentResolver: delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER#content://media/external/images/media#_data = ?#/storage/emulated/0/DCIM/Screenshots/pic1.jpg
What else should I put in my code to solve this issue. Thanks in advance.
To delete media files in android 10 and above, You can try contentresolver API.
You need to pass the media's content uri to the 'contentResolver.delete()' method & you will be able to do it easily.
Here is my post on how to do that using Java - Scoped Storage in Android — Writing & Deleting Media Files

Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Snapchat/Snapchat-441126967.jpg: open failed: EACCES (Permission denied)

Please I keep getting this error, each time I try to pick an image on my flutter application. Taking a live picture from my camera on the app works well though, but when I try to access my storage the error keeps coming forth.
Note:
I use the image_picker package to pick both images and access the camera.
Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Snapchat/Snapchat-441126967.jpg: open failed: EACCES (Permission denied)
Everything was working well, of a sudden I started having this issue. I have done the following.
In my android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Also I have the requestLegacyExternalStorage set to true.
In Pubsec.yaml
permission_handler: ^8.1.2
image_picker:
Flutter Version
Flutter 2.0.5 • channel stable
Block of code to pick image
Future<void> _pickImage(ImageSource source) async {
print(":: Pick Image executed :::");
try {
await Permission.storage.request();
var status = await Permission.storage.status;
print(":::: The Status of the permission is ::::");
print(status);
if (status.isGranted) {
File selected =
await ImagePicker.pickImage(source: source, imageQuality: 30);
final bytes = selected.readAsBytesSync();
String _img64 = base64Encode(bytes);
setState(() {
_imageFile = selected;
base64 = _img64;
});
uploadFile();
}
} catch (error) {
print(error);
}
}
The permission is granted as shown by the permission package but I still keep getting the error at the end of the day when I select an image from by device gallery.
So eventually I found out that the problem was peculiar to only android 10 devices. The way forward was to update my image_picker package to the latest version which is at this moment 0.8.1+3.
And everything worked very well.
It's happening because of android storage permission. They solved the problem in the latest version. Please check out there read me file .
Starting with version 0.8.1 the Android implementation support to pick (multiple) images on Android 4.3 or higher.
No configuration required - the plugin should work out of the box.
It is no longer required to add android:requestLegacyExternalStorage="true" as an attribute to the tag in AndroidManifest.xml, as image_picker has been updated to make use of scoped storage.

adding lines to your AndroidManifest.xml file - fail to provide permission

I already placed the permission to allow external storage of document and potentially list all PDF documents. Despite adding the lines to your AndroidManifest.xml file, the error message keeps appearing when widget is launched.
Please advise how to fix this error message.
Exception has occurred.
FileManagerError (
Try to add thes lines to your AndroidManifest.xml file
`<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>`
`<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>`
and grant storage permissions to your applicaion from app settings
FileSystemException: Directory listing failed, path = '/storage/emulated/0/' (OS Error: Permission denied, errno = 13))
This is the method where the error comes from:
void getFiles() async { //asyn function to get list of files
List<StorageInfo> storageInfo = await PathProviderEx.getStorageInfo();
var root = storageInfo[0].rootDir; //storageInfo[1] for SD card, geting the root directory
var fm = FileManager(root: Directory(root)); //
files = await fm.filesTree(
excludedPaths: ["/storage/emulated/0/Android"],
extensions: ["pdf"] //optional, to filter files, list only pdf files
);
setState(() {}); //update the UI
}
After adding the following permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Add the following package to the pubspec.yaml file:
https://github.com/Baseflow/flutter-permission-handler
dependencies:
permission_handler: ^5.0.1+1
Then you can request storage permission:
if (await Permission.storage.request().isGranted) {
// Either the permission was already granted before or the user just granted it.
}

How to correct read file from "Downloads" folder in NativeScript app?

I write a Android app on NativeScript.
I have defined required permissions in App_Resources/Android/AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
I know that it's not enough for getting access to storage, so I have requested permission in runtime and check it in System settings.
After downloading some image by browser to Downloads folder I wanna read it in my app. Initially I got a file path by nativescript-mediafilepicker, but for know I use following snippet:
console.log(fs.File.exists('/storage/emulated/0/Download/7zoVG9uML7AyjwHl.png'));
const file = fs.File.fromPath('/storage/emulated/0/Download/7zoVG9uML7AyjwHl.png');
file.readSync(e => console.log('ERROR ' + e));
which print in console:
JS: true
JS: ERROR Error: java.io.FileNotFoundException: /storage/emulated/0/Download/7zoVG9uML7AyjwHl.png: open failed: EACCES (Permission denied)
What I do wrong?
UPDATE
I added WRITE_EXTERNAL_STORAGE permission, but it has no effect:
permissions.requestPermissions(p).then(async () => {
const publicDirectory = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
const fullPath = `${publicDirectory}/7zoVG9uML7AyjwHl.png`;
const file = fs.File.fromPath(fullPath);
file.readSync(e => console.log('ERROR ' + e));
});
JS: ERROR Error: java.io.FileNotFoundException: /storage/emulated/0/Download/7zoVG9uML7AyjwHl.png: open failed: EACCES (Permission denied)
So, the solution is:
Add in AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Add in AndroidManifest.xml
<application android:requestLegacyExternalStorage="true" ...>
Get file by string path:
const file = fs.File.fromPath('/storage/emulated/0/Download/7zoVG9uML7AyjwHl.png');
file.readSync(e => console.log('ERROR ' + e));
Good morning,
My system details:
Plugin: #nativescript-community/ui-document-picker
Ubuntu 20.04.3 LTS
NativeScript CLI 8.1.4
NVM 0.35.3
Node v16.13.0
NPM 8.1.0
Android 11 API Level 30
"#nativescript/core": "~8.1.1",
"#nativescript-community/ui-document-picker": "^1.1.7",
"#nativescript/android": "8.1.1",
When Open filepicker and select a PDF file from downloads folder the file.readSync sayas error:
Error: java.io.FileNotFoundException: /storage/emulated/0/Download/Test.pdf: open failed: EACCES (Permission denied)
And when call file.read says error:
Error: ReadTask returns no result.
This just happens only when targetSdkVersion is set to 30 and not in 29, but now to publish and app in Google App Store needs targetSdkVersion 30.
Thanks
I think to access downloads directory you need to add also the write permission to your manifest i.e. android.permission.WRITE_EXTERNAL_STORAGE
then you can build the path to your file progamaticlly using
new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/7zoVG9uML7AyjwHl.png");
Add in AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:requestLegacyExternalStorage="true" ...>
Use nativescript-permissions plugin : https://www.npmjs.com/package/nativescript-permissions
example:
Permissions.requestPermissions(
[
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
]).then(() => {
//your code
};
For Android 10 API 29 you can't use getExternalStorageDirectory() because it was deprecated. more info here: https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory()
Instead of:
const file = fs.File.fromPath('/storage/emulated/0/Download/7zoVG9uML7AyjwHl.png');
Use:
import {Utils} from '#nativescript/core';
const file = Utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() +"/7zoVG9uML7AyjwHl.png"

Categories

Resources