I am really having a hard time making my app work for Android 12.
I have these lines in AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
...
android:requestLegacyExternalStorage="true"
... >
<activity
android:name=".Home"
android:exported="true"
android:screenOrientation="sensorPortrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I also have this snippet in Home.java:
if(Settings.Global.getInt(getContentResolver(), Settings.Global.AUTO_TIME, 0) == 1) {
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We dont have permission so prompt the user
ActivityCompat.requestPermissions(
this,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_PERMISSION_CODE);
tvStatus.setText("Please click \"allow\" for the app to work seamlessly. Then, click on the play button below");
fbtnEnter.setVisibility(View.VISIBLE);
onClick(this, fbtnEnter, "restart");
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) {
}
}
}
This is the method I use to download the file:
static String[] getUsesAndUpdateData(String filename) throws IOException {
FileInputStream fin = null;
int ch;
StringBuffer sb = new StringBuffer();
String text = "";
String[] fragments = new String[0];
String accepted_characters = "0123456789ABCDEF,;";
boolean check = true;
try{
fin = new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/" + filename);
while((ch = fin.read()) != -1) {
check = accepted_characters.contains("" + (char) ch);
if (check) {
sb.append((char) ch);
} else sb.append((char) 88);
}
text = sb.toString();
fragments = text.split(";");
}
catch (FileNotFoundException e) {
log("File not found" + e);
}
catch (IOException ioe) {
log("Exception while reading file " + ioe);
}
finally {
// close the stream using close method
try {
if (fin != null) fin.close();
}
catch (IOException ioe) {
log("Error while closing stream: " + ioe);
}
}
return fragments;
}
Checking the logcat, I can see the ff:
2022-10-04 18:51:02.593 11754-11754/com.samp.test I/MVPX: File not foundjava.io.FileNotFoundException: /storage/emulated/0/Documents/text1.txt: open failed: EACCES (Permission denied)
2022-10-04 18:51:02.602 11754-11754/com.samp.test I/MVPX: File not foundjava.io.FileNotFoundException: /storage/emulated/0/Documents/text2.txt: open failed: EACCES (Permission denied)
2022-10-04 18:51:02.614 11754-11754/com.samp.test I/MVPX: File not foundjava.io.FileNotFoundException: /storage/emulated/0/Documents/text1.txt: open failed: EACCES (Permission denied)
2022-10-04 18:51:02.629 11754-11754/com.samp.test I/MVPX: File not foundjava.io.FileNotFoundException: /storage/emulated/0/Documents/text2.txt: open failed: EACCES (Permission denied)
2022-10-04 18:51:02.656 11754-11754/com.samp.test I/MVPX: File not foundjava.io.FileNotFoundException: /storage/emulated/0/Documents/text1.txt: open failed: EACCES (Permission denied)
2022-10-04 18:51:02.669 11754-11754/com.samp.test I/MVPX: File not foundjava.io.FileNotFoundException: /storage/emulated/0/Documents/text2.txt: open failed: EACCES (Permission denied)
2022-10-04 18:51:02.696 11754-11754/com.samp.test I/MVPX: File not foundjava.io.FileNotFoundException: /storage/emulated/0/Documents/text1.txt: open failed: EACCES (Permission denied)
2022-10-04 18:51:02.712 11754-11754/com.samp.test I/MVPX: File not foundjava.io.FileNotFoundException: /storage/emulated/0/Documents/text2.txt: open failed: EACCES (Permission denied)
I tried several options and workarounds, but when I run the app in Android 12, I always get an EACCES permission error. I run out of things to try to make it work. Please help me with this. Thank you.
Related
I have developed a android system application to copy file from /sdcard/download/test.txt to /cache/xyz/ location.
I am able to copy the file to /cache/ , but bot into /cache/xyz/ location ,
Getting below error :
java.io.FileNotFoundException: /cache/xyz/test.txt: open failed: EACCES (Permission denied)
File packageFile = new File(Environment.getDownloadCacheDirectory() + "/xyz/test.txt");
File downloadedFile = new File(Environment.getExternalStorageDirectory() + "/test.txt");
if (packageFile.exists()) {
Log.d(TAG, "TEST -> File in Cache Exists");
} else {
Log.d(TAG, "TEST -> File in Cache is Empty");
}
packageFile.canWrite();
if (downloadedFile.exists()) {
Log.d(TAG, "TEST -> packageFile in downloadedFile Exists");
FileChannel source = null;
FileChannel dest = null;
try {
source = (new FileInputStream(downloadedFile)).getChannel();
dest = (new FileOutputStream(packageFile)).getChannel();
count += dest.transferFrom(source, count, size-count);
catch (Exception e) {
Log.d(TAG, "TEST -> Failed to copy update file into internal storage: " + e);
}
} else {
Log.d(TAG, "TEST -> File DO NOT Exists");
}
Manifest :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
For API 23+ you need to request the read/write permissions even if they are already in your manifest.
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
/**
* Checks if the app has permission to write to device storage
*
* If the app does not has permission then the user will be prompted to grant permissions
*
* #param activity
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
via: Exception 'open failed: EACCES (Permission denied)' on Android
I am trying to access a file in Android, below is the code:
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"sos/xconfig.txt");
if(!file.exists())
return text.toString();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
I am also checking for read and write permission like this before reading :
if (ContextCompat.checkSelfPermission(activity, permission)
== PackageManager.PERMISSION_DENIED) {
// Requesting the permission
ActivityCompat.requestPermissions(activity,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE },
requestCode);
return false;
}
else {
return true;
}
But I am still getting an error at this line : new FileReader(file).
Error : java.io.FileNotFoundException: /storage/emulated/0/sos/xconfig.txt: open failed: EACCES (Permission denied)
NOTE: I have already added uses-permission in menifest file.
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Are u using Android Q ?
If that is the case you need to add this attribute in your AndroidManifest file :
<manifest ... >
<!-- This attribute is "false" by default on apps targeting Android Q. -->
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>
Source : Exception 'open failed: EACCES (Permission denied)' on Android
everyone. I tried to access file "/sys/class/power_supply/Battery/current_now" to retrieve the battery current in Huawei P20.
f = File("/sys/class/power_supply/Battery/current_now");
if (f.exists()) {
return OneLineReader.getValue(f, true)
However i get the error of
java.io.FileNotFoundException: /sys/class/power_supply/Battery/current_now: open failed: EACCES (Permission denied)
i already grant permission of read and write to storage.
private val PERMISSIONS_STORAGE = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
fun verifyStoragePermissions(activity: Activity) {
// Check if we have write permission
val permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
)
}
}
Anyone has the hint for this problem?
You need turn off selinux.
Use adb shell "setenforce 0" and then you can read it from your app
in the mainFest it has
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and for newer OS it checks the permission
ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
if it does not return true, it will request the permission
ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 5);
and only when it has the permission it will try to create the file
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String sysDownloadPath = directory.getPath();
try {
if (!directory.exists() && !directory.mkdirs()) {
//shouldn't happen in here
/* Checks if external storage is available for read and write */
String state = Environment.getExternalStorageState();
String externalStorageAvailable = (Environment.MEDIA_MOUNTED.equals(state)) ?
"ext-ST available" :
"ext-ST NOT available"; }
} catch (Exception e) { //<=== did not caught exception here
}
String fileName = ‘SD_20170404.pdf’;
String newFilePath = (sysDownloadPath + "/" + fileName);
File newFile = new File(newFilePath);
try {
newFile.createNewFile();// <=== throws at this one
} catch (Exception e) {
fatal Exception: java.lang.Exception: open failed: EACCES (Permission denied), newFile.getPath():/storage/emulated/0/Download/SD_20170404.pdf
It happens (not always but) with OS 5, 6 and 7. any suggestion? thanks!
The following code is what I wrote for saving crash message in a local file
try {
long timestamp = System.currentTimeMillis();
String time = formatter.format(new Date());
String fileName = "crash-" + time + "-" + timestamp + ".log";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String path = Environment
.getExternalStorageDirectory()
.getAbsolutePath()
+ "/crash/";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(path + fileName);
fos.write(sb.toString().getBytes());
fos.close();
}
return fileName;
} catch (Exception e) {
Log.e(TAG, "an error occured while writing file...", e);
}
But When my app crashed.The log show me some errors.It's shown as follows:
java.io.FileNotFoundException: /storage/emulated/0/crash/crash-2016-09-04-15-35-25-1472974525277.log: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:459)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
at com.attendance.exception.CrashHandler.saveCrashInfo2File(CrashHandler.java:212)
at com.attendance.exception.CrashHandler.handleException(CrashHandler.java:135)
at com.attendance.exception.CrashHandler.uncaughtException(CrashHandler.java:88)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:445)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
at com.attendance.exception.CrashHandler.saveCrashInfo2File(CrashHandler.java:212)
at com.attendance.exception.CrashHandler.handleException(CrashHandler.java:135)
at com.attendance.exception.CrashHandler.uncaughtException(CrashHandler.java:88)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
I have add permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
What is the problem?Thanks for you help!
This is the answer i gave for the same issue in another question.
https://developer.android.com/training/permissions/requesting.html
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.
you need to manually code the permission granting part (besides defining it in the manifest).
the following is a snippet from developer.android.com
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
Change your gradel like :
targetSdkVersion 22
instead of
targetSdkVersion 23
now no need to get user permission run time in marshmallow too ..
i hope it work