无法在 Android 6 上的 phone 内存上写入

Can't write on phone memory on Android 6

这是我的 AndroidManifest:

  <permission
    android:name="nl.hgrams.passenger.permission.MAPS_RECEIVE" android:protectionLevel="signature" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.LOCATION_HARDWARE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<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"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="nl.hgrams.passenger.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

这是我在 logcat 中遇到的错误:

 12-02 10:03:30.532: W/System.err(11348): java.io.IOException: open failed: EACCES (Permission denied)
 12-02 10:03:30.532: W/System.err(11348):   at java.io.File.createNewFile(File.java:939)
 12-02 10:03:30.532: W/System.err(11348):   at nl.hgrams.passenger.utils.Utils.appendLog(Utils.java:829)
 12-02 10:03:30.532: W/System.err(11348):   at nl.hgrams.passenger.PSLocationCenter.onCreate(PSLocationCenter.java:233)
 12-02 10:03:30.532: W/System.err(11348):   at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
 12-02 10:03:30.532: W/System.err(11348):   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
 12-02 10:03:30.532: W/System.err(11348):   at android.app.ActivityThread.-wrap1(ActivityThread.java)
 12-02 10:03:30.532: W/System.err(11348):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
 12-02 10:03:30.532: W/System.err(11348):   at android.os.Handler.dispatchMessage(Handler.java:102)
 12-02 10:03:30.532: W/System.err(11348):   at android.os.Looper.loop(Looper.java:148)
 12-02 10:03:30.532: W/System.err(11348):   at android.app.ActivityThread.main(ActivityThread.java:5417)
 12-02 10:03:30.532: W/System.err(11348):   at java.lang.reflect.Method.invoke(Native Method)
 12-02 10:03:30.532: W/System.err(11348):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 12-02 10:03:30.533: W/System.err(11348):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 12-02 10:03:30.533: W/System.err(11348): Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
 12-02 10:03:30.533: W/System.err(11348):   at libcore.io.Posix.open(Native Method)
 12-02 10:03:30.533: W/System.err(11348):   at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
 12-02 10:03:30.533: W/System.err(11348):   at java.io.File.createNewFile(File.java:932)
 12-02 10:03:30.533: W/System.err(11348):   ... 12 more
 12-02 10:03:30.535: W/System.err(11348): java.io.FileNotFoundException: /storage/emulated/0/Passenger/passenger_log.txt: open failed: EACCES (Permission denied)
 12-02 10:03:30.535: W/System.err(11348):   at libcore.io.IoBridge.open(IoBridge.java:452)
 12-02 10:03:30.535: W/System.err(11348):   at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
 12-02 10:03:30.535: W/System.err(11348):   at java.io.FileWriter.<init>(FileWriter.java:58)
 12-02 10:03:30.535: W/System.err(11348):   at nl.hgrams.passenger.utils.Utils.writeToLog(Utils.java:905)
 12-02 10:03:30.535: W/System.err(11348):   at nl.hgrams.passenger.utils.Utils.appendLog(Utils.java:849)
 12-02 10:03:30.535: W/System.err(11348):   at nl.hgrams.passenger.PSLocationCenter.onCreate(PSLocationCenter.java:233)
 12-02 10:03:30.535: W/System.err(11348):   at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
 12-02 10:03:30.535: W/System.err(11348):   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
 12-02 10:03:30.535: W/System.err(11348):   at android.app.ActivityThread.-wrap1(ActivityThread.java)
 12-02 10:03:30.535: W/System.err(11348):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
 12-02 10:03:30.535: W/System.err(11348):   at android.os.Handler.dispatchMessage(Handler.java:102)
 12-02 10:03:30.535: W/System.err(11348):   at android.os.Looper.loop(Looper.java:148)
 12-02 10:03:30.536: W/System.err(11348):   at android.app.ActivityThread.main(ActivityThread.java:5417)
 12-02 10:03:30.536: W/System.err(11348):   at java.lang.reflect.Method.invoke(Native Method)
 12-02 10:03:30.536: W/System.err(11348):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 12-02 10:03:30.536: W/System.err(11348):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 12-02 10:03:30.536: W/System.err(11348): Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
 12-02 10:03:30.536: W/System.err(11348):   at libcore.io.Posix.open(Native Method)
 12-02 10:03:30.536: W/System.err(11348):   at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
 12-02 10:03:30.536: W/System.err(11348):   at libcore.io.IoBridge.open(IoBridge.java:438)
 12-02 10:03:30.536: W/System.err(11348):   ... 15 more

我发现我需要这样做,在 api 23+:

中声明它们
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
    Manifest.permission.READ_EXTERNAL_STORAGE, 
    Manifest.permission.WRITE_EXTERNAL_STORAGE 
 }; 

但如果我这样设置权限,它无法识别权限。

cannot resolve symbol Manifest.permission.Read/Write...

从 Android 6.0(API 级别 23)开始,用户在应用 运行 时向应用授予权限,而不是在安装应用时。这种方法简化了应用程序安装过程,因为用户在安装或更新应用程序时不需要授予权限。它还使用户可以更好地控制应用程序的功能;例如,用户可以选择授予相机应用访问相机但不访问设备位置的权限。用户可以随时通过转到应用程序的设置屏幕来撤销权限。

来源http://developer.android.com/intl/es/training/permissions/requesting.html

用户需要通过进入设置-> 应用程序-> app_name-> 权限手动提供权限,或者您必须在应用程序运行时以编程方式手动请求权限。

试试这个代码

import android.Manifest;
import android.app.Activity;
import android.support.v4.app.ActivityCompat;

public class ClassName {

private static final int REQUEST_EXTERNAL_STORAGE = 1;

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    public void requestPermissions(Activity activity) {
        ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
    }
}

它对我有用。