【问题标题】:Android - java.io.FileNotFoundExceptionAndroid - java.io.FileNotFoundException
【发布时间】:2014-08-31 19:51:25
【问题描述】:

当我将位图图像插入文件目录时,它显示文件未找到异常并显示是一个目录。

这是我的代码:

            File mFolder = new File(getFilesDir() + "/sample");

            if (!mFolder.exists()) {
                mFolder.mkdir();
            }
             FileOutputStream fos = null;
             try {
                 fos = new FileOutputStream(mFolder);
                 bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);

                 fos.flush();
                 fos.close();
              //   MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
             }catch (FileNotFoundException e) {

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

                 e.printStackTrace();
             }

Logcat:

07-12 01:08:05.434: W/System.err(8170): java.io.FileNotFoundException: /data/data/com.sample.sam/files/sample(Is a directory)
07-12 01:08:05.434: W/System.err(8170):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
07-12 01:08:05.434: W/System.err(8170):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239)
07-12 01:08:05.444: W/System.err(8170):     at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
07-12 01:08:05.444: W/System.err(8170):     at java.io.FileOutputStream.<init>(FileOutputStream.java:77)

【问题讨论】:

    标签: android filenotfoundexception


    【解决方案1】:

    在写入流之前,您必须创建文件。

    File mFolder = new File(getFilesDir() + "/sample");
    File imgFile = new File(mFolder.getAbsolutePath() + "/someimage.png");
    if (!mFolder.exists()) {
        mFolder.mkdir();
    }
    if (!imgFile.exists()) {
        imgFile.createNewFile();
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(imgFile);
        bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
         e.printStackTrace();
    }
    

    【讨论】:

      【解决方案2】:

      如果要创建文件而不是目录,请使用file.createNewFile()。 如果路径也不存在,您可能还需要使用mkDirs()

      【讨论】:

        【解决方案3】:

        您正在打开目录本身进行写入。这不起作用。您需要在目录中指定一个文件,例如:

        fos = new FileOutputStream(new File(mFolder, "myfile"));
        

        【讨论】:

        • 我添加了代码。它显示 NullPointerException 这一行::bitmap.compress(Bitmap.CompressFormat.PNG,70, fos);
        • 这是一个无关的问题。也许bitmapnull
        【解决方案4】:
        File sdcard = Environment.getExternalStorageDirectory();
        File dir = new File(sdcard.getAbsolutePath() + "/text/");
        dir.mkdir();
        File file = new File(dir, "sample.txt");
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file);
            os.write(enterText.getText().toString().getBytes());
            os.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        

        【讨论】:

        • 请始终将您的答案放在上下文中,而不仅仅是粘贴代码。有关详细信息,请参阅here
        猜你喜欢
        • 2016-09-20
        • 2012-12-12
        • 1970-01-01
        • 2015-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多