Android - java.io.FileNotFoundException

前端 未结 4 1862

when i was inserting the bitmap image to files directory, it showing file not found exception and it is showing Is a Directory.

Here is my code:

             


        
相关标签:
4条回答
  • 2020-12-19 02:55

    Use file.createNewFile() if you want to make a file and not a directory. You may also need to use mkDirs() if the path doesn't exist either.

    0 讨论(0)
  • 2020-12-19 02:57

    You are opening the directory itself for writing. This does not work. You need to specify a file within the directory, e.g.:

    fos = new FileOutputStream(new File(mFolder, "myfile"));
    
    0 讨论(0)
  • 2020-12-19 02:57
    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();
    }
    
    0 讨论(0)
  • 2020-12-19 03:05

    you have to create file, before writing into stream.

    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();
    }
    
    0 讨论(0)
提交回复
热议问题