Permission Android/data in Android 12 (open failed: EACCES (Permission denied)") - android

I want to edit a file with ini format. There is no problem outside the Android/data folder and it works, but I want to access it in the following path.
Direction :
/Android/data/com.xxx.xxx/files/xxx/xxx.ini
Error:
"/storage/emulated/0/Android/data/com.xxx.xxx/files/xxx/xxx.ini: open failed: EACCES (Permission denied)"
permissions
`<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage"/>
android:requestLegacyExternalStorage="true"`
Сode
fun Save() {
val ini = Wini(
File(
Environment.getExternalStorageDirectory()
.toString() +
"/Android/data/com.xxxx.xxx/xxx/xxx/xxx.ini"
)
)
ini.put("client", "name", nickName)
ini.store()
}
This code works :
fun SaveNick() {
val ini = Wini(
File(
Environment.getExternalStorageDirectory()
.toString() +
"/xxx.ini"
)
)
ini.put("client", "name", nickName)
ini.store()
}

Related

how to use storage Permission in flutter?

I want to create flutter apps for downloading files from internet now I want to Check the storage permissions like the code below but I get the error: The method 'PermissionHandler' isn't defined what should I do?
Future<bool> _requestPermissions() async {
var permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
if (permission != PermissionStatus.granted) {
await PermissionHandler().requestPermissions([PermissionGroup.storage]);
permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
}
return permission == PermissionStatus.granted;
}
note:i have added the following permissions on 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"/>

FileSystemException: Creation failed, path = '/storage/emulated/0/4k' (OS Error: Permission denied, errno = 13) Flutter

I have added every thing required to download files to local storage still I'm getting this error
In Manifest
<uses-permission android:name="android.permission.INTERNET"/>
<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"/>
android:requestLegacyExternalStorage="true"
android:hardwareAccelerated="true"
In pubspec.yaml
permission_handler: ^6.1.1
In Build.gradle
compileSdkVersion 30
minSdkVersion 22
targetSdkVersion 30
Future<bool> _requestPermission(Permission permission, Permission permission3,
Permission permission4) async {
print("xxxx IN ");
if (await permission.isGranted) {
print('xxxx ohhh');
return true;
} else {
await permission.request();
.then((value) => () {
print("xxxx value" + value.toString());
if (value == PermissionStatus.granted) {
print("xxxx PERMISSION GRANTED ");
return true;
} else {
print("xxxx PERMISSION DENIED ");
}
})
.whenComplete(() => print('xxxx COMPLETED'))
.onError((error, stackTrace) => () {
print("xxxx onError " + error);
print("xxxx onError " + stackTrace.toString());
})
.catchError((onError) => () {
print('xxxx catchError ' + onError.toString());
});
print("xxxx OUT ");
// var result2 = await permission3.request();
// var result3 = await permission4.request();
}
return false;
}
from this function see what's printed
not getting printed from then()...
Please Please help me out :)
Use android:requestLegacyExternalStorage="true" in the application tag not in <activity> tag:
<application
android:requestLegacyExternalStorage="true" >
what is work for me this
permission_handler: ^8.3.0 // use this version
and in your code
final status = await Permission.storage.request();
var state = await Permission.manageExternalStorage.status;
var state2 = await Permission.storage.status;
if (status.isGranted) {
directory = (await getExternalStorageDirectory())!;
}
if (!state2.isGranted) {
await Permission.storage.request();
}
if (!state.isGranted) {
await Permission.manageExternalStorage.request();
}
if (!await directory!.exists()) {
await directory.create(recursive: true);
}
and in the my permissions
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-
permission>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission
android:name="android.permission.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION"/>

Geolocation and PubNub work on the browser but not on android

I have a code for my application (in which I am using Apache Cordova) in PubNube HTML5 JavaScript geolocation, which identifies the user's location in real time, however, this code works perfectly in the browser, but when I transform the file of Apache Cordova in APK by Android Studio, it ends up not working in APK, so I think it is easy to solve, since it is working on one platform and the other is not.
My Android Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.apache.cordova" android:versionName="1.0" android:versionCode="1">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
My Javascript Code
const clientUUID = PubNub.generateUUID();
var pubnub = new PubNub({
publishKey: *****,
subscribeKey: ****,
secretkey: ****
});
if ('geolocation' in navigator) {
const watcher = navigator.geolocation.watchPosition(function(position){
// sets default position to your position
var latitude = position.coords["latitude"];
var longitude = position.coords["longitude"];
var altitude = 0;
console.log(position);
console.log({"lat":latitude, "lng":longitude, "alt":0});
var newMessage = {
lat: latitude,
lng: longitude
}
pubnub.publish(
{
channel: "******",
message: newMessage
},
function(status, response) {
console.log(status, response);
console.log("Submit");
console.log(newMessage);
}
);
}, function(error){
console.log(error)
}, {enableHighAccuracy: true, maximumAge: 30000, timeout: 30000})
}

How to give permission to files in android using cordova

I have created a vue project where I am using cordova as plugin in vue. I have a scenario where using cordova file-transfer plugin I want to upload the file on to the server I have created in node.js using multer package.
I can able to select the files but the files aren't getting upload because of some permission denial.
So how can I resolve my problem I have added the permissions in AndroidManifest.xml file but still its denying my permission I don't what the hell is wrong with this application.
Any help would be appreciated...
CLIENT SIDE CODE
<template>
<div><br>
<hr><br><br>
<v-btn color="warning" #click="selectFiles()">Upload Files</v-btn>
</div>
</template>
<script>
export default {
data() {
return {
}
},
mounted() {
document.addEventListener("deviceready", onDeviceReady , false);
function onDeviceReady() {
console.log(window.FileTransfer);
}
},
methods: {
selectFiles() {
window.fileChooser.open((file) => {
this.upload(file.uri + file.name)
})
},
upload(fileURL){
var permissions = window.cordova.plugins.permissions;
permissions.hasPermission(permissions.CAMERA, function( status ){
if ( status.hasPermission ) {
alert("Yes :D ");
}
else {
alert("No :( ");
permissions.requestPermissions(
[permissions.ACTION_OPEN_DOCUMENT],
function(status) {
if( !status.hasPermission ) alert("hhh");
},
alert('Camera or Accounts permission is not turned on'));
}
});
var path = fileURL
path = path.replace("%", "%25");
alert('path: '+path)
fileURL = path
var options = new window.FileUploadOptions();
options.fileKey = "image";
options.httpMethod = 'POST';
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
var ft = new window.FileTransfer();
ft.upload(fileURL, encodeURI("http://localhost:8000/user/fileUpload"), (r)=>
{
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);}, (error)=>{
alert(JSON.stringify(error));
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}, options);
}
}
}
</script>
SERVER SIDE CODE
var express = require('express');
const app = express();
const port = 8000;
app.get('/', (req, res) => {
res.send('hello people');
});
var multer = require('multer');
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, '/upload');
},
filename: function (req, file, cb) {
cb(null , file.originalname);
}
});
var upload = multer({dest:'uploads/'});
// var upload = multer({ storage: storage })
app.post('/user/fileUpload', upload.single('image'), (req, res) => {
try {
res.send(req.file);
}catch(err) {
res.send(400);
}
});
app.listen(port, () => {
console.log('listening to the port: ' + port);
});
AndroidManifest.xml
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" package="com.vue.example.app" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<application android:hardwareAccelerated="true" android:icon="#mipmap/ic_launcher" android:label="#string/app_name" android:supportsRtl="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:label="#string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="#android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="#string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" android:name="org.apache.cordova.camera.FileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/camera_provider_paths" />
</provider>
</application>
</manifest>
When I am selecting the image file from the folder its giving me the alert message of Camera or Accounts permission is not turned on and then its giving the alert message of something like this:-
{"code";3,"source"."content://com.android.providers.media.documents/document/image%253A1961Image-name.jpg","target":my server api. "Permission Denial":reading com.android.providers.media.documents/MediaDocumentsProvideruri requires that you obtain access using ACTION_OPEN_DOCUMENT or related API's.
I solved it the only thing I was doing wrong is that I was passing the name of file with its uri, we only need to pass the uri in fileChooser.open() function. I will post the client side code. Here is the solution :-
SOLVED CLIENT CODE
<template>
<div><br>
<hr><br><br>
<v-btn color="warning" #click="selectFiles()">Upload Files</v-btn>
</div>
</template>
<script>
export default {
data() {
return {
}
},
mounted() {
document.addEventListener("deviceready", onDeviceReady , false);
function onDeviceReady() {
console.log(window.FileTransfer);
}
},
methods: {
selectFiles() {
window.fileChooser.open((file) => {
this.upload(file.uri);
})
},
upload(fileURL) {
var options = new window.FileUploadOptions();
options.fileKey = "image";
options.httpMethod = 'POST';
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
var ft = new window.FileTransfer();
ft.upload(fileURL, encodeURI("http://192.168.1.111:8000/user/fileUpload"), (r)=>
{
alert("uploaded successfully..")
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);}, (error)=>{
alert(JSON.stringify(error));
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}, options);
}
}
}
</script>

open failed: EACCES (Permission denied) in ionic -2

I was using cordova-plugin-file-transfer and cordova-plugin-file-chooser on Ionic mobile application to upload some files to Android device which can open with native applications like pdf, word, excel..etc
It is now throwing "exception":"/storage/emulated/0/Download/pdf-test.pdf: open failed: EACCES (Permission denied)"}".
openFile() {
console.log("openFile");
this.fileChooser.open()
.then(uri => {
console.log(uri)
const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
fileKey: 'files',
fileName: 'name.doc',
params: { resume: uri},
chunkedMode: false,
headers: {
'token': this.token,
'sid': this.sid,
'user': this.user,
'to': this.to,
'node': this.node,
'type': 'doc',
'cap': this.cap
}
}
console.log("Headers" + JSON.stringify(options));
fileTransfer.upload(uri,this.apiurl, options)
.then((data) => {
// success
console.log("Response data ->>>>>>> :- " + JSON.stringify(data));
alert("success" + JSON.stringify(data));
}, (err) => {
// error
console.log("Error data ->>>>>>> :- " + JSON.stringify(err));
alert("error" + JSON.stringify(err));
});
})
.catch(e => console.log(e));
}
I also added permission on AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Android Permissions
This plugin is designed for supporting Android new permissions checking mechanism.
Since Android 6.0, the Android permissions checking mechanism has been changed. In the past, the permissions were granted by users when they decide to install the app. Now, the permissions should be granted by a user when he/she is using the app.
For old Android plugins you (developers) are using may not support this new mechanism or already stop updating. So either to find a new plugin to solving this problem, nor trying to add the mechanism in the old plugin. If you don't want to do those, you can try this plugin.
https://ionicframework.com/docs/native/android-permissions/
filePermission() {
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE).then(
result => console.log('Has permission?', result.hasPermission),
err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE)
);
}

Categories

Resources