Write file to sdcard in android

后端 未结 4 606
半阙折子戏
半阙折子戏 2020-12-21 02:05

I want to create a file on sdcard. Here I can create file and read/write it to the application, but what I want here is, the file should be saved on specific folder of sdcar

相关标签:
4条回答
  • 2020-12-21 02:36

    It's pretty easy to create folder and write file on sd card in android

    Code snippet

        String ext_storage_state = Environment.getExternalStorageState();
                File mediaStorage = new File(Environment.getExternalStorageDirectory()
                        + "/Folder name");
                if (ext_storage_state.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
                    if (!mediaStorage.exists()) {
                        mediaStorage.mkdirs();
                    } 
                    //write file writing code..
    
    try {
    
                    FileOutputStream fos=new FileOutputStream(file name);
                    try {
                        fos.write(filename.toByteArray());
                        fos.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }           
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
                else
                {
                    //Toast message sd card not found..
                }
    
    0 讨论(0)
  • 2020-12-21 02:37

    Here's an example from my code:

    try {
        String filename = "abc.txt";
        File myFile = new File(Environment
                .getExternalStorageDirectory(), filename);
        if (!myFile.exists())
            myFile.createNewFile();
        FileOutputStream fos;
        byte[] data = string.getBytes();
        try {
            fos = new FileOutputStream(myFile);
            fos.write(data);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    

    And don't forget the:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    
    0 讨论(0)
  • 2020-12-21 02:58

    Try like this,

    try {
        File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder");
        if (!newFolder.exists()) {
            newFolder.mkdir();
        }
        try {
            File file = new File(newFolder, "MyTest" + ".txt");
            file.createNewFile();
        } catch (Exception ex) {
            System.out.println("ex: " + ex);
        }
    } catch (Exception e) {
        System.out.println("e: " + e);
    }
    
    0 讨论(0)
  • 2020-12-21 02:59

    Note: 'getExternalStorageDirectory()' actually gives you a path to /storage/emulated/0 and not the actual sdcard

    'String path = Syst.envr("SECONDARY_STORAGE");' gives you a path to the sd card

    0 讨论(0)
提交回复
热议问题