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

喜欢而已 提交于 2019-12-12 02:19:09

问题


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?


回答1:


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.




回答2:


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" />



回答3:


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



来源:https://stackoverflow.com/questions/39462667/titanium-android-external-storage-create-new-directory-and-then-write-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!