尝试将日志写入SD卡时出现异常

时间:2016-06-03 07:20:22

标签: android logging log4j

我正在使用log4j将日志写入Android中的外部存储。

public static Logger getLogger(Class clazz) {
        final LogConfigurator logConfigurator = new LogConfigurator();
        String logsFolderName = Environment.getExternalStorageDirectory().toString() + File.separator + "player/logs/";
        String logFileName = "player.log." + getCurrentDateString();
        if (!new File(logsFolderName).exists())
            logsFolderName = "/tmp/";

        logConfigurator.setFileName(logsFolderName + logFileName);
        logConfigurator.setRootLevel(Level.ALL);
        logConfigurator.setLevel("org.apache", Level.ALL);
        logConfigurator.setUseFileAppender(true);
        logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n");
        logConfigurator.setMaxFileSize(1024 * 1024 * 5);
        logConfigurator.setImmediateFlush(true);
        logConfigurator.configure();
        Logger log = Logger.getLogger(clazz);
        return log;
    }

的build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "app.com.blynq.player"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
...
dependencies {
...
compile group: 'log4j', name: 'log4j', version: '1.2.17'
}

我在运行时遇到以下异常:

Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method) 
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)

我已获得读取和写入外部目录的权限

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.com.blynq.player">


    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen">

        </activity>

        <receiver
            android:name="app.com.blynq.services.BootReceiver"
            android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_ON" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <activity android:name=".RegisterActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

导致问题的原因是什么?

1 个答案:

答案 0 :(得分:0)

每当logs文件夹始终解析为logsFolderName =“/ tmp /”时,就会出现此异常,因为不存在此类文件夹。有关创建tmp文件的信息,请参阅此post