我正在开发华硕nexus 7,我正在将数据写入文件,写入权限如下所示添加到清单中,但在运行时logcat显示 错误表示所需的权限不存在。
注意:安装在其他设备上时,相同的代码正在运行,但它不适用于华硕Nexus 7.
请告诉我如何解决此错误
清单:
<?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>
**检查权限:
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
}
}
如何创建文件
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;
}
}
*更新
**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)
答案 0 :(得分:0)
从API级别23开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时。您有两个解决方案可以解决您的问题快速的是将targetApi降低到22,第二个是在运行时请求权限,如下例所示。
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED) {
//do the things}
else {
requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },AnyNumber);
}
答案 1 :(得分:-1)
从Android文档检查此链接。 http://developer.android.com/training/permissions/requesting.html
基本上,当用户批准请求时,您必须在活动中定义onRequestPermissionsResult()回调。检查是否授予/拒绝许可。
您的代码中不清楚代码中的位置。