Android - java.io.FileNotFoundException

前端 未结 4 1873

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 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();
    }
    

提交回复
热议问题