I'm getting an exception that file open permission is denied when I'm trying to open a FileInputStream.
File myFile = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/test/test.txt");
try {
FileInputStream inStream = new FileInputStream(myFile); // crash
Here's the stacktrace:
System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/test/test.txt: open failed: EACCES (Permission denied)
System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:456)
System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:76)
...
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:442)
And yes, I do have the permissions in my manifest:
<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE" />
Android is case sensitive. Replace ANDROID.PERMISSION with android.permission:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Also, you should only need one of those permissions. If you are planning on writing to external storage, you should not also need READ_EXTERNAL_STORAGE.
Related
https://github.com/itinance/react-native-fs
code
import RNFS from 'react-native-fs';
RNFS.mkdir(RNFS.ExternalDirectoryPath + dir)
logcat
05-06 22:43:41.693 W/System.err: java.lang.Exception: Directory could not be created
05-06 22:43:41.697 W/System.err: at com.rnfs.RNFSManager.mkdir(RNFSManager.java:680)
This error occurs after compilation,
But it is normal when debugging,
ok, permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
With Android 10, when calling
androidx.appcompat.widget.AppCompatImageView.setImageURI(uri)
I see this silent exception in the logs:
java.io.FileNotFoundException: /data/user/0/com.myapp/files/1585300417998.jpg: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:496)
at java.io.FileInputStream.<init>(FileInputStream.java:159)
at java.io.FileInputStream.<init>(FileInputStream.java:115)
at android.content.ContentResolver.openInputStream(ContentResolver.java:1192)
at android.graphics.ImageDecoder$ContentResolverSource.createImageDecoder(ImageDecoder.java:282)
at android.graphics.ImageDecoder.decodeDrawableImpl(ImageDecoder.java:1743)
at android.graphics.ImageDecoder.decodeDrawable(ImageDecoder.java:1736)
at android.widget.ImageView.getDrawableFromUri(ImageView.java:1009)
at android.widget.ImageView.resolveUri(ImageView.java:978)
at android.widget.ImageView.setImageURI(ImageView.java:555)
at androidx.appcompat.widget.AppCompatImageView.setImageURI(AppCompatImageView.java:116)
at com.myapp.LargeImageView.setImage(LargeImageView.java:88)
at com.myapp.LargeImageView.onSizeChanged(LargeImageView.java:56)
These two permissions are declared in my manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I am working on wifi related app, which takes the user giving username password and need to write(edit/append)in a file which is located in system/etc/wifi/ path.
I searched in stackoverflow and got some answers and then tried but there is no luck. This is rooted device. I did remount and su too. I can able to read the same file. I placed all the permissions in manifest too. What I missed?
try {
String[] command = new String[]{"/system/bin/ls", "-l",
"/system/etc/wifi/wpa_supplicant.conf" };
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
String output = "";
String line;
while ((line = reader.readLine()) != null) {
output.concat(line + "\n");
Log.w("myApp", "[[output]]:" + line);
}
reader.close();
process.waitFor();
String[] command2 = new String[]{"/system/etc/wifi/chmod", "777",
"/system/etc/wifi/wpa_supplicant.conf" };
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(new String[]{"/system/bin/sh", "-c", "echo \"12345678\" > /system/etc/wifi/wpa_supplicant.conf"});
File path= Environment.getDataDirectory();
FileWriter fw = new FileWriter(new File("/system/etc/wifi/wpa_supplicant"));
fw.write('9');
fw.close();
Log.d(TAG, "File Saved Successfully !!!");
//display file saved message
/*Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();*/
} catch (Exception e) {
e.printStackTrace();
}
Here is my manifest permissions (outside application tag and some more permissions also there for some other purpose ).
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
And below error I am getting,
01-01 06:13:53.565: W/myApp(11205): [[output]]:-rw-rw-rw- root root 58 2000-01-01 03:56 wpa_supplicant.conf
01-01 06:13:53.580: W/System.err(11205): java.io.FileNotFoundException: /system/etc/wifi/wpa_supplicant: open failed: EROFS (Read-only file system)
01-01 06:13:53.580: W/System.err(11205): at libcore.io.IoBridge.open(IoBridge.java:452)
01-01 06:13:53.580: W/System.err(11205): at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
01-01 06:13:53.580: W/System.err(11205): at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
01-01 06:13:53.581: W/System.err(11205): at java.io.FileWriter.<init>(FileWriter.java:42)
01-01 06:13:53.581: W/System.err(11205): at com.intel.sunnypoint.headless.HeadlessService.WriteBtn(HeadlessService.java:450)
01-01 06:13:53.581: W/System.err(11205): at com.intel.sunnypoint.headless.HeadlessService.setupWifiAPConfig(HeadlessService.java:386)
01-01 06:13:53.581: W/System.err(11205): at com.intel.sunnypoint.headless.HeadlessService$HttpServiceThread$WiFiPageHandler.handle(HeadlessService.java:1094)
01-01 06:13:53.581: W/System.err(11205): at org.apache.http.protocol.HttpService.doService(HttpService.java:248)
01-01 06:13:53.581: W/System.err(11205): at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:192)
01-01 06:13:53.581: W/System.err(11205): at com.intel.sunnypoint.headless.HeadlessService$HttpServiceThread.run(HeadlessService.java:663)
01-01 06:13:53.581: W/System.err(11205): Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
01-01 06:13:53.581: W/System.err(11205): at libcore.io.Posix.open(Native Method)
01-01 06:13:53.581: W/System.err(11205): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
01-01 06:13:53.582: W/System.err(11205): at libcore.io.IoBridge.open(IoBridge.java:438)
01-01 06:13:53.582: W/System.err(11205): ... 9 more
Did you have root access? Without root access only the mechanism of flashing an update (via recovery or OTA) can modify /system. To be accepted, such an update archive must be signed with the same key as the ROM – which is something only the developer of the ROM can do.
There might be a way via a custom recovery which lets you edit all file systems directly (twrp). But without knowing what device it is, it cannot be said if a matching recovery exists. Apart from that, it's either rooting the device or not editing any file within /system.
If you have root access, you should execute commands as root! su command should be used
Runtime.getRuntime().exec("su");
or a full path to su.bin in each command.
I am trying to use an Android app to upload a file to a node js server on my laptop. While it works fine with html, when trying to do the same in Andorid I get the following error.
The server is a simple Node server which seems to be working fine when I use other methods of posting files.
Error in upload with ID: 10ff84ba-0768-4423-a955-7a1a4f68d1aa. /init.rc: open failed: EACCES (Permission denied)
java.io.FileNotFoundException: /init.rc: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at com.alexbbb.uploadservice.BinaryUploadFile.getStream(BinaryUploadFile.java:34)
at com.alexbbb.uploadservice.BinaryUploadTask.writeBody(BinaryUploadTask.java:29)
at com.alexbbb.uploadservice.HttpUploadTask.upload(HttpUploadTask.java:131)
at com.alexbbb.uploadservice.HttpUploadTask.run(HttpUploadTask.java:65)
at com.alexbbb.uploadservice.UploadService.onHandleIntent(UploadService.java:127)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:442)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at com.alexbbb.uploadservice.BinaryUploadFile.getStream(BinaryUploadFile.java:34)
at com.alexbbb.uploadservice.BinaryUploadTask.writeBody(BinaryUploadTask.java:29)
at com.alexbbb.uploadservice.HttpUploadTask.upload(HttpUploadTask.java:131)
at com.alexbbb.uploadservice.HttpUploadTask.run(HttpUploadTask.java:65)
at com.alexbbb.uploadservice.UploadService.onHandleIntent(UploadService.java:127)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
01-01 08:17:17.382 20517-25217/jorc.com.fileupload2 W/com.alexbbb.uploadservice.BinaryUploadTask: Error in uploadId 5d59b0bb-5f5f-4926-b17d-87a8e408af4f on attempt 1. Waiting 1s before next attempt java.io.FileNotFoundException: /init.rc: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at com.alexbbb.uploadservice.BinaryUploadFile.getStream(BinaryUploadFile.java:34)
at com.alexbbb.uploadservice.BinaryUploadTask.writeBody(BinaryUploadTask.java:29)
at com.alexbbb.uploadservice.HttpUploadTask.upload(HttpUploadTask.java:131)
at com.alexbbb.uploadservice.HttpUploadTask.run(HttpUploadTask.java:65)
at com.alexbbb.uploadservice.UploadService.onHandleIntent(UploadService.java:127)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
Trying later I get the following confirmation from logcat but still no file is uploaded to my server:
01-01 08:57:43.032 1837-1837/jorc.com.fileupload2 I/UploadServiceDemo: Upload with ID 2b47d6b5-7bf6-4cf0-aeec-462b20c40ffd is completed: 200, <!DOCTYPE html>
<html>
<head>
<title>File Uploader</title>
<link href="app.css" rel="stylesheet">
</head>
It seems you are using this repo from githib:
https://github.com/alexbbb/android-upload-service
in this case go to MainActivity.java file and add
.addHeader("X-Parse-Application-Id", "your_key")
.addHeader("X-Parse-REST-API-Key", "your_key2")
under
.addHeader("file-name", new File(fileToUploadPath).getName())
That's all you need to see your file being uploaded to parse.com.
To make it easier on device add this:
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage(serverResponseMessage);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
Under this which shows you the uploaded file URL.
public void onCompleted(String uploadId, int serverResponseCode, String serverResponseMessage) {
progressBar.setProgress(0);
It is clearly written in your logcat Permission denied, you need to give READ permission in your manifest.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I'm having the following error on a few of my users:
Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
at [my communication HttpUrlConnection request]
... 12 more
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
at libcore.io.Posix.getaddrinfo(Native Method)
at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
... 25 more
Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
... 28 more
The app requires internet to do it's most basic function, I don't think a user have removed the permission using root or something like that.
Additionally, this error was sent to me with my own error reporting tool. I have made the upload with the registered error and at that moment the permission was available.
There is no restriction on device model of android version that has the error.
Example of a part of the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
[...]
Any clue is appreciated,
Thanks in advance.
This will help you.
Write the following in your onCreate method
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.CAMERA};
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
And get this method
public static boolean hasPermissions(Context context, String... permissions)
{
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null)
{
for (String permission : permissions)
{
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED)
{
return false;
}
}
}
return true;
}
The application will ask for only those permission which the application needs, that too only once. If it works upvote and do let me know. It worked for me
HttpUrlConnection request also requires android.permission.ACCESS_NETWORK_STATE. Do you also have this permission listed in the manifest?
Please post your manifest file so we can see that android.permission.ACCESS_NETWORK_STATE, and android.permission.INTERNET are both listed without any spelling, capitalization, syntax errors.
The issue can also happen if a user has rooted their Android device and tinkered with the permissions. How do you know the user has not rooted the device? Can you reproduce the problem on any of your own devices?
See more detail on this issue here: SecurityException: Permission denied (missing INTERNET permission?)