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

3

1

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 so restricted to always write files on internal memory even after using Ti.Filesystem.externalStorageDirectory?

Can someone who has ever been able to create a new directory programmatically on SD card in Android write the answer here or no one ever needs to write something on external SD card?

Celik answered 13/9, 2016 at 5:22 Comment(0)
D
2

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.

Dinka answered 13/9, 2016 at 5:31 Comment(1)
thanks for answer...but my question is specific to Titanium SDK. I will edit the question.Celik
R
0

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" />
Ruderal answered 13/9, 2016 at 5:31 Comment(5)
Environment.getExternalStorageDirectory() does not return sdcard path always. On some devices, it returns the internal storage path. I don't recommend this.Dinka
read not accepted answer in this questionDinka
but your device is found sdcard den first create directory to sdcard and not found den internal storageRuderal
some devices has two storage devices, sdcard and sdcard-ext, and Environment.getExternalStorageDirectory() returns sdcard which is internal storage even if device has external card inserted.Dinka
i have tested on device. ex. both device is sdcard and internal 1.device android 5.1 and storage path storage/sdcard0 is external and .../sdcard1 my code create dir on sdcard0 2.device android 4.1 is storage path storage/sdcard0 and ext-sdcard and create dir ext -sdcardRuderal
C
0

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().

Catanddog answered 13/9, 2016 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.