java.io.FileNotFoundException:/storage/emulated/0/crash/crash-2016-09-04-15-35-25-1472974525277.log:open failed:ENOENT(没有这样的文件或目录)

时间:2016-09-04 07:53:36

标签: android

以下代码是我为在本地文件中保存崩溃消息而编写的代码

        try {
            long timestamp = System.currentTimeMillis();
            String time = formatter.format(new Date());
            String fileName = "crash-" + time + "-" + timestamp + ".log";
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                String path = Environment
                        .getExternalStorageDirectory()
                        .getAbsolutePath()
                        + "/crash/";
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                FileOutputStream fos = new FileOutputStream(path + fileName);
                fos.write(sb.toString().getBytes());
                fos.close();
            }
            return fileName;
        } catch (Exception e) {
            Log.e(TAG, "an error occured while writing file...", e);
        }

但是当我的应用程序崩溃时。日志显示了一些错误。它显示如下:

java.io.FileNotFoundException: /storage/emulated/0/crash/crash-2016-09-04-15-35-25-1472974525277.log: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:459)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
at com.attendance.exception.CrashHandler.saveCrashInfo2File(CrashHandler.java:212)
at com.attendance.exception.CrashHandler.handleException(CrashHandler.java:135)
at com.attendance.exception.CrashHandler.uncaughtException(CrashHandler.java:88)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:445)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:127) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:116) 
at com.attendance.exception.CrashHandler.saveCrashInfo2File(CrashHandler.java:212) 
at com.attendance.exception.CrashHandler.handleException(CrashHandler.java:135) 
at com.attendance.exception.CrashHandler.uncaughtException(CrashHandler.java:88) 
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693) 
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690) 

我在AndroidManifest.xml中添加了权限

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

有什么问题?谢谢你的帮助!

2 个答案:

答案 0 :(得分:2)

这是我在另一个问题中针对同一问题给出的答案。

https://developer.android.com/training/permissions/requesting.html

从Android 6.0(API级别23)开始,用户在应用运行时向应用授予权限,而不是在安装应用时授予权限。

您需要手动编写权限授予部分(除了在清单中定义它之外)。

以下是developer.android.com的片段

if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
    != PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

    // Show an expanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.

} else {

    // No explanation needed, we can request the permission.

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);

    // MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}
}

答案 1 :(得分:1)

将您的成绩改为:

targetSdkVersion 22

而不是

targetSdkVersion 23

现在也无需在棉花糖中获得用户权限运行时间..

我希望它有效