I am developing on Asus nexus 7, and i am writing data to file, the permission for writing is added as shown below in the manifest, but at run time the logcat displays an error saying that the required permission is not there.
NOTE: the same code is working when installed on other devices, but it does not work on Asus Nexus 7.
please let me know how to fix this error
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.bt_11" >
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ActMain"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
**check for the permission:
protected void onPreExecute() {
super.onPreExecute();
Log.w(TAG, CSubTag.msg("ATRx.onPreExecute"));
....
....
....
this.mPerWES = Manifest.permission.WRITE_EXTERNAL_STORAGE;
int hasWESPer = ContextCompat.checkSelfPermission(getApplicationContext(), this.mPerWES);
if ((hasWESPer == PackageManager.PERMISSION_GRANTED)) {
this.mLogFile = IOCtrl.createFile("Test_00.txt");
} else {
Log.e(TAG, CSubTag.msg("ATRx.onPreExecute", "missing Permission: " + this.mPerWES));//logcat displays this line
}
}
how the file is created
public static File createFile(String fileName) {
Log.w(TAG, CSubTag.msg("createFile"));
String state;
if (IOCtrl.isExternalStorageMounted()) {
//Log.v(TAG, CSubTag.subBullet("createFile", "MEDIA_MOUNTED_READ_ONLY"));
File dir = new File(IOCtrl.ROOT_DIR + File.separator + IOCtrl.DIR_NAME);
boolean dirCreated = dir.mkdirs();
if (dirCreated) {
Log.d(TAG, "dir: " + dir.getAbsolutePath() + " created");
} else {
Log.d(TAG, "dir: " + dir.getAbsolutePath() + " already exists");
}
File file = new File(dir, fileName);
boolean fileExists = file.exists();
if (fileExists) {
Log.d(TAG, "file: " + fileName + " already exists");
} else {
Log.d(TAG, "file: " + fileName + " will be created");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, CSubTag.subBullet("createFile", "Error creating file: " + e.getMessage()));
return null;
}
}
return file;
} else {
Log.e(TAG, CSubTag.msg("createFile", "media storage is not available"));
return null;
}
}
*update
**logcat**:
11-27 10:05:42.564 16376-16376/com.example.com.bt_11 W/IOCtrl: +++++ isExternalStorageMounted() +++++
11-27 10:05:42.573 16376-16376/com.example.com.bt_11 D/IOCtrl: isExternalStorageMounted(): -> media state: mounted
11-27 10:05:42.573 16376-16376/com.example.com.bt_11 D/IOCtrl: dir: /storage/emulated/0/CAN_BUS already exists
11-27 10:05:42.574 16376-16376/com.example.com.bt_11 D/IOCtrl: file: Test_00.txt will be created
11-27 10:05:42.574 16376-16376/com.example.com.bt_11 W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
11-27 10:05:42.574 16376-16376/com.example.com.bt_11 W/System.err: at java.io.File.createNewFile(File.java:939)
11-27 10:05:42.574 16376-16376/com.example.com.bt_11 W/System.err: at com.example.com.bt_11.IOCtrl.createFile(IOCtrl.java:87)
11-27 10:05:42.574 16376-16376/com.example.com.bt_11 W/System.err: at com.example.com.bt_11.ActConnect2$ATRx.onPreExecute(ActConnect2.java:856)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:604)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at com.example.com.bt_11.ActConnect2$ATConnect.onPostExecute(ActConnect2.java:705)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at com.example.com.bt_11.ActConnect2$ATConnect.onPostExecute(ActConnect2.java:577)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:651)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at android.os.Looper.loop(Looper.java:148)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at libcore.io.Posix.open(Native Method)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: at java.io.File.createNewFile(File.java:932)
11-27 10:05:42.575 16376-16376/com.example.com.bt_11 W/System.err: ... 14 more
11-27 10:05:42.576 16376-16376/com.example.com.bt_11 E/IOCtrl: <<createFile>>: Error creating file: open failed: EACCES (Permission denied)