Titanium - Android external storage - create new directory and then write files into it

后端 未结 3 1539
忘了有多久
忘了有多久 2021-01-27 00:30

Can we not simply create new directory programmatically on external SD card (not internal memory of device) in Android and can we not write files on SD card?

Is Titanium

相关标签:
3条回答
  • 2021-01-27 01:02

    You can use System class to get storage variables like below

    To get the internal SD card you can use

    String extStore = System.getenv("EXTERNAL_STORAGE");
    File f_exts = new File(extStore);
    

    To get the external SD card you can use

    String secStore = System.getenv("SECONDARY_STORAGE");
    File f_secs = new File(secStore);
    

    You can choose where to create folder and which one to use.

    EDIT

    Environment.getExternalStorageDirectory() Details

    • Return the primary shared/external storage directory.
    • This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

    Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

    Reference from Environment

    Hope it'll help.

    0 讨论(0)
  • 2021-01-27 01:04

    try this create folder and file in sdcard

    File localFile = new File(Environment.getExternalStorageDirectory(),"/data/create_new_folder_name/");
                if (!localFile.exists()) {
                    localFile.mkdir();
                }
                try{
                    File gpxfile = new File(localFile, "yourfilename.xyz");
                    FileWriter writer = new FileWriter(gpxfile,true);
                    writer.append("your file text");
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    

    manifest.xml permission

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    0 讨论(0)
  • 2021-01-27 01:11

    Use the Storage Access Framework to write on the micro SD card. Google for ACTION_OPEN_DOCUMENT_TREE and ACTION_OPEN_DOCUMENT.

    Besides that you can write in a normal way in one app specific directory on the micro SD card. Have a look at the second entry returned by getExternalFilesDirs().

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