应用程序将在三星SM-P585设备版本6.0棉花糖中崩溃

时间:2017-10-30 05:04:19

标签: android android-6.0-marshmallow android-permissions

您好我的应用程序与所有设备一起工作正常,但仅在三星SM-P585 device.i上面提到错误报告错误报告 请帮忙......

这里是gradle文件

apply plugin:'com.android.application'
android    {        
compileSdkVersion 25     
buildToolsVersion '25.0.0'

defaultConfig {

    minSdkVersion 15
    targetSdkVersion 25
    versionCode 25
    versionName "25.0"
    multiDexEnabled true

}

dexOptions {
    javaMaxHeapSize "4g"
}

packagingOptions {
    exclude 'bin/AndroidManifest.xml'
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
        'proguard-rules.pro'
    }
  }  
 }

dependencies {
compile project(':library')
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/android-zoom-view.jar')
compile files('libs/achartengine-1.0.0.jar')
compile files('libs/mpandroidchartlibrary-2-1-1.jar')
compile files('libs/lefu.jar')
compile files('libs/HL-AndroidLib.jar')
compile files('libs/HL-DataDecoderLib.jar')
compile 'com.android.support:appcompat-v7:22.2.0'
compile files('libs/libGoogleAnalyticsServices.jar')
compile 'com.google.android.gms:play-services:6.5.87'
compile files('libs/itextpdf-5.3.3.jar')
compile 'com.android.support:multidex:1.0.1'
}

错误报告

10-26 23:58:33.211 W/System.err(23790):java.io.FileNotFoundException: /storage/emulated/0/WatIf/WatIf_Log.txt: open failed: EACCES (Permission denied)
10-26 23:58:33.211 W/System.err(23790): at libcore.io.IoBridge.open(IoBridge.java:452)
10-26 23:58:33.211 W/System.err(23790): at java.io.FileOutputStream.(FileOutputStream.java:87)
10-26 23:58:33.211 W/System.err(23790): at java.io.FileWriter.(FileWriter.java:58)
10-26 23:58:33.211 W/System.err(23790): at com.watifhealth.community.log.LogToFile.Log(LogToFile.java:64)
10-26 23:58:33.211 W/System.err(23790): at com.watifhealth.community.log.Logger.Log(Logger.java:36)
10-26 23:58:33.211 W/System.err(23790): at com.watifhealth.community.calculation.CommonMethods.GetPersonAge(CommonMethods.java:42)
10-26 23:58:33.211 W/System.err(23790): at com.watifhealth.community.BaseActivity.SessionSP(BaseActivity.java:258)
10-26 23:58:33.211 W/System.err(23790): at com.watifhealth.community.BaseActivity.onCreate(BaseActivity.java:105)
10-26 23:58:33.211 W/System.err(23790): at com.watifhealth.community.PatientsFragmentActivity.onCreate(PatientsFragmentActivity.java:174)
10-26 23:58:33.211 W/System.err(23790): at android.app.Activity.performCreate(Activity.java:6904)
10-26 23:58:33.211 W/System.err(23790): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
10-26 23:58:33.211 W/System.err(23790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
10-26 23:58:33.211 W/System.err(23790): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
10-26 23:58:33.211 W/System.err(23790): at android.app.ActivityThread.access$1100(ActivityThread.java:229)
10-26 23:58:33.211 W/System.err(23790): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
10-26 23:58:33.211 W/System.err(23790): at android.os.Handler.dispatchMessage(Handler.java:102)
10-26 23:58:33.211 W/System.err(23790): at android.os.Looper.loop(Looper.java:148)
10-26 23:58:33.211 W/System.err(23790): at android.app.ActivityThread.main(ActivityThread.java:7399)
10-26 23:58:33.211 W/System.err(23790): at java.lang.reflect.Method.invoke(Native Method)
10-26 23:58:33.211 W/System.err(23790): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
10-26 23:58:33.211 W/System.err(23790): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
10-26 23:58:33.211 W/System.err(23790): Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
10-26 23:58:33.211 W/System.err(23790): at libcore.io.Posix.open(Native Method)
10-26 23:58:33.211 W/System.err(23790): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
10-26 23:58:33.211 W/System.err(23790): at libcore.io.IoBridge.open(IoBridge.java:438)
10-26 23:58:33.211 W/System.err(23790): ... 20 more
10-26 23:58:33.221 I/Directory:(23790): Already exists.!!
10-26 23:58:33.221 W/System.err(23790): java.io.FileNotFoundException: /storage/emulated/0/WatIf/WatIf_Log.txt: open failed: EACCES (Permission denied)
10-26 23:58:33.221 W/System.err(23790): at libcore.io.IoBridge.open(IoBridge.java:452)
10-26 23:58:33.221 W/System.err(23790): at java.io.FileOutputStream.(FileOutputStream.java:87)
10-26 23:58:33.221 W/System.err(23790): at java.io.FileWriter.(FileWriter.java:58)
10-26 23:58:33.221 W/System.err(23790): at com.watifhealth.community.log.LogToFile.Log(LogToFile.java:64)
10-26 23:58:33.221 W/System.err(23790): at com.watifhealth.community.log.Logger.Log(Logger.java:36)
10-26 23:58:33.221 W/System.err(23790): at com.watifhealth.community.calculation.CommonMethods.GetPersonAge(CommonMethods.java:42)
10-26 23:58:33.221 W/System.err(23790): at com.watifhealth.community.BaseActivity.SessionSP(BaseActivity.java:258)
10-26 23:58:33.221 W/System.err(23790): at com.watifhealth.community.BaseActivity.onCreate(BaseActivity.java:105)
10-26 23:58:33.221 W/System.err(23790): at com.watifhealth.community.PatientsFragmentActivity.onCreate(PatientsFragmentActivity.java:174)
10-26 23:58:33.221 W/System.err(23790): at android.app.Activity.performCreate(Activity.java:6904)

这是我声明所有权限的清单文件

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
    android:name="android.permission.CAMERA"
    android:required="true" />
<uses-permission android:name="android.permission.READ_LOGS" />

<!-- for glucometer -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- for bp machine -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

对于marshmallow版本,我还添加了运行时权限

  private static final int REQUEST_EXTERNAL_STORAGE = 1;
  private static final int REQUEST_LOCATION = 2;
  private static final int REQUEST_CAMERA = 3;
  private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.ACCESS_COARSE_LOCATION
  };
  private static String[] PERMISSIONS_LOCATION = {
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION
  };


  public void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, 
  Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int permission1 = ActivityCompat.checkSelfPermission(activity, 
  Manifest.permission.ACCESS_COARSE_LOCATION);
    int permission2 = ActivityCompat.checkSelfPermission(activity, 
  Manifest.permission.CAMERA);


    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );


    }

    if (permission1 != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_LOCATION,
                REQUEST_LOCATION
        );

    }

    if (permission2 != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_CAMERA
        );
    }
    count++;

}

这是在设备内存中创建文件。

public class LogToFile implements ILogger {

public static FileHandler logger = null;
String filename = "WatIf_Log";

static boolean isExternalStorageAvailable = false;
static boolean isExternalStorageWriteable = false;
static String state = Environment.getExternalStorageState();
private PrintWriter writer;

@Override
public void Log(Exception e) {

    String message = Log.getStackTraceString(e);

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        isExternalStorageAvailable = isExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        isExternalStorageAvailable = true;
        isExternalStorageWriteable = false;
    } else {
        isExternalStorageAvailable = isExternalStorageWriteable = false;
    }

    File dir = new File(
            Environment.getExternalStorageDirectory(),
            "WatIf");
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        if (!dir.exists()) {
            Log.d("Dir Created:", "Directory is created");
            dir.mkdir();
        } else {
            Log.i("Directory:", "Already exists.!!");
        }

        File logFile = new File(dir, "" + filename + ".txt");

        if (!logFile.exists()) {
            try {

                Log.d("File Create:", "Log File Created ");
                logFile.createNewFile();

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        try {

            writer = new PrintWriter(new FileWriter(logFile, false));
            Date date = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            writer.write("Logged at"
                    + cal.get(Calendar.DAY_OF_MONTH) +  "-"
                    + cal.get(Calendar.MONTH) +  "-"
                    + cal.get(Calendar.YEAR) +  " , "
                    + String.valueOf(cal.get(Calendar.HOUR) + ":"
                    + cal.get(Calendar.MINUTE) + ":"
                    + cal.get(Calendar.SECOND) + "\n"));
            writer.write(message);
            writer.println("--");
            writer.flush();
            writer.close();
            Log.i("File:", "Data written on File.!!");
        } catch (Exception er) {
            er.printStackTrace();
        }
    }
}

@Override
public void Log(String strMessage) {

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        isExternalStorageAvailable = isExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        isExternalStorageAvailable = true;
        isExternalStorageWriteable = false;
    } else {
        isExternalStorageAvailable = isExternalStorageWriteable = false;
    }

    File dir = new File(
            Environment.getExternalStorageDirectory(),
            "WatIf");
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        if (!dir.exists()) {
            Log.d("Dir Created:", "Directory is created");
            dir.mkdir();
        } else {
            Log.i("Directory:", "Already exists.!!");
        }

        File logFile = new File(dir, "" + filename + ".txt");

        if (!logFile.exists()) {
            try {

                Log.d("File Create:", "Log File Created ");
                logFile.createNewFile();

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        try {

            writer = new PrintWriter(new FileWriter(logFile, false));
            Date date = new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            writer.write("Logged at"
                    + cal.get(Calendar.DAY_OF_MONTH) + "-"
                    + cal.get(Calendar.MONTH) + "-"
                    + cal.get(Calendar.YEAR) + " , "
                    + String.valueOf(cal.get(Calendar.HOUR) + ":"
                    + cal.get(Calendar.MINUTE) + ":"
                    + cal.get(Calendar.SECOND) + "\n"));
            writer.write(strMessage);
            writer.println("--");
            writer.flush();
            writer.close();
            Log.i("File:", "Data written on File.!!");
        } catch (Exception er) {
            er.printStackTrace();
        }
    }
}

@Override
public void Log(Context context, String Title, String strMessage) {
    // TODO Auto-generated method stub

}

@Override
public void ShowInformationCloud(Context context, String strMessage) {

}
}

提前致谢...

2 个答案:

答案 0 :(得分:2)

您还应该覆盖onRequestPermissionResult,了解用户使用“拒绝”或“允许”操作执行的操作。

@Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
        super.onRequestPermissionsResult(requestCode,permissions,grantResults);
    }

答案 1 :(得分:0)

检查所有条件

1)在Application类或活动onCreate()中:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

2)禁用从设备到计算机的文件传输。如果启用,应用程序将无法访问SD卡。