为什么有时会抛出FileNotFoundException [英] why sometime it throws FileNotFoundException

查看:137
本文介绍了为什么有时会抛出FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该代码在大多数情况下都有效,但是有时会引发异常.无法弄清楚是什么原因造成的.

The code works for most of the time, but some time it throws exception. Couldn't figure out what could cause it.

做什么是在

/storage/emulated/0/Download/theFileName.jpg

/storage/emulated/0/Download/theFileName.jpg

向其中写入数据(从确实存在的sourceFile中),但是新创建的文件出现文件不存在"异常.

and write data to it (from sourceFile which does exist), but got "file not exist" exception for the newly created file.

(清单中确实有uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE", and uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE).

File sourceFile = new File(theSourceFileFullPath);
if (sourceFile.exists()) {
    File downloadDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    String downloadPath = downloadDirectory.getPath();
    String newFilePath = (downloadPath + "/" + fileName);
    File newFile = new File(newFilePath);
    try {
        FileInputStream in = new FileInputStream(sourceFile);

        // ava.io.FileNotFoundException: 
        //     /storage/emulated/0/Download/theFileName.jpg: open failed: ENOENT (No such file or directory) 
        // exception at this line       
        FileOutputStream out = new FileOutputStream(newFile);
        //......
     } catch (Exception e) {}
}

推荐答案

我可以想到此行为的以下可能原因:

I can think of the following possible reasons for this behavior:

  1. 不能仅由于SD卡上没有可用空间而无法创建新文件.下次遇到此问题时,请尝试通过文件管理器或设置"->存储"查看是否至少有一些可用空间.我怀疑如果这是在用户设备上发生的事情,您是否可以对此做些事情.
  2. 由于某些操作系统故障或SD卡物理上未连接,因此SD卡不可用,因此无法创建文件.再一次-除了显示带有错误消息的Toast之外,您无能为力.
  3. 如何生成文件路径的fileName部分? 有时,由于文件名包含不受支持的文件,因此无法创建文件(通过此特定设备)字符.例如.我有带Android 4.1.2的HTC Desire X,如果文件名包含冒号,则无法使用类似于您的代码创建文件.尝试另一种方式来生成文件名,看看它是否有所不同.
  4. 在内部创建文件之前,您确定Downloads目录是否存在?编写如下内容:

  1. The new file could not be created simply because there is no free space on SD card. The next time you encounter this issue, try to see if you have at least some available space via file manager or Settings -> Storage. I doubt you can do something about it if this is what happens on your user's device.
  2. The SD card is not available due to some OS glitch or it's physically not attached, thus the file could not be created. Once again - there is nothing you can do about it, other than showing some Toast with error message.
  3. How do you generate the fileName part of your file's path? Sometimes the files can not be created because the filename contains unsupported (by this particular device) characters. E.g. I have HTC Desire X with Android 4.1.2 which can not create files with the code similar to yours if the filename contains colon. Try another way to generate the filename and see if it makes a difference.
  4. Did you make sure that the Downloads directory exists before creating the file inside it? Write something like the following:

if (!downloadDirectory.exists()) {
    downloadDirectory.mkdirs();
}

  • WRITE_EXTERNAL_STORAGE权限被视为危险在Android 6上,因此用户可以随时将其撤消. 请确保用户在写信之前未撤消此许可权文件.
  • 始终关闭I/O流使用完后.将finally子句添加到try-catch块中,并关闭其中的inout流:

  • The WRITE_EXTERNAL_STORAGE permission is considered dangerous on the Android 6, thus it can be revoked by the user at any time. Make sure user didn't revoke this permission before writing to the file.
  • Always close the I/O stream when you finished working with it. Add the finally clause to your try-catch block and close both in and out streams in it:

    finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) { }
        }
    
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) { }
        }
    }
    

  • 最后一个-最后我没有选择权了. :)我想如果您从多个线程写入同一文件,也会出现此问题.如果是这种情况,请尝试同步对文件编写代码的访问.
  • The last one - and finally I'm out of options. :) I suppose this issue could also arise if you write to the same file from multiple threads. If that's the case, try to synchronize access to your file-writing code.
  • 这篇关于为什么有时会抛出FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆