Android saving file to external storage

后端 未结 12 859
抹茶落季
抹茶落季 2020-11-22 03:12

I have a little issue with creating a directory and saving a file to it on my android application. I\'m using this piece of code to do this :

String filename         


        
12条回答
  •  失恋的感觉
    2020-11-22 03:56

    You need a permission for this

    
    

    and method:

    public boolean saveImageOnExternalData(String filePath, byte[] fileData) {
    
        boolean isFileSaved = false;
        try {
            File f = new File(filePath);
            if (f.exists())
                f.delete();
            f.createNewFile();
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(fileData);
            fos.flush();
            fos.close();
            isFileSaved = true;
            // File Saved
        } catch (FileNotFoundException e) {
            System.out.println("FileNotFoundException");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOException");
            e.printStackTrace();
        }
        return isFileSaved;
        // File Not Saved
    }
    

提交回复
热议问题