1

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)
Amrmsmb
  • 1
  • 27
  • 104
  • 226

2 Answers2

0

From API level 23 onwards users grant permissions to apps while the app is running, not when they install the app. You have two solutions for your problem. The quick one is to lower targetApi to 22 and the second is to ask for permissions at run time as shown in the following example.

if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED) {
   //do the things} 
else {
    requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },AnyNumber);
}
Kuffs
  • 35,581
  • 10
  • 79
  • 92
Jithin Sunny
  • 3,352
  • 4
  • 26
  • 42
  • would u please tell m eth 2nd solution – Amrmsmb Nov 27 '15 at 08:55
  • would u please have a look at the update section above..now i receive those logcat output...and i am being asked to allow the app to acees the external storage..but the file does not want to be created – Amrmsmb Nov 27 '15 at 09:09
-1

check this link from android docs. http://developer.android.com/training/permissions/requesting.html
Basically, when user approves request, you have to define onRequestPermissionsResult() callback in your activity & check if permission is granted/denied. It is not clear from your code where this is there in your code.

RaViMete
  • 161
  • 3