Guidance on Android file storage

我只是一个虾纸丫 提交于 2019-12-12 20:23:18

问题



I need to write an Android app that would download sounds from the internet for further use.
Where should i store these sounds? Is Sqlite an option? And what's the best way to load and play these files?
Thanks in advance.


回答1:


I would recommend putting the files on the SD Card, if one exists. One thing to note, though, is that you should never hard code a path to the SD card. You would likely want to do something along the lines of:

try {
        //Create handle to SD card drectory
        exportDirectory = new File(Environment.getExternalStorageDirectory() + "/MyDir");

        //Verify export path exists.  Create otherwise.
        if (exportDirectory.exists() == false) {
            if (exportDirectory.mkdirs() == false) {

                //Directory structure could not be created.
                //Message the user here
                return false;
            }
        }

        //Create handle to SD card file.
        exportFile = new File(exportDirectory.getAbsolutePath() + "/whatever.mp3");

        //Do whatever here

} catch(Exception e) {
        //Exception.  Message user and bail
        return false;
}

From there it is a simple matter of transferring whatever data you want into the file. An SQLite database would likely be overkill for this app, unless you plan on storing a LOT of extra information. There is also no guarantee, unless you store the files directly into the database as BLOBs, that the user won't mess with the file system between application runs. Though, in the case of MP3s and the like, track / artist type information can be stored directly into the file via ID3 tags instead of using a database.

As for playing the files back, consult the Android documentation on their audio APIs. There is a ton of great information out there, definitely more than I can repeat here.




回答2:


I'd say the best place to store them is as individual files on the sdcard under your own folder i.e. File("/sdcard/com.example.myapp/sound12345.mp3");


EDIT: please see phobos51594's answer which is much more complete than mine.




回答3:


It's best to save them directly on the file system. Why would you want to store it in a database? are there additional information you need to store together with it? (Like titles, descriptions etc)? If not, I'd write it directly into the filesystem.

Basically you could save it into the data folder of your app (where the shared preferences, and sqlite db are stored too). However, I personally think this should be discouraged, as it reduces the available memonry on the /data partition (which is already tooo small on Android phones. Most phones have less than 50 MB free on data (this means: less than 50 MB for apps, which can't be partly moved to SD card).

You should always safe such fines on SD card if possible, so you don't lower free space on data partition too much. Even with Froyo's A2SD feature, not all data of an installed app is moved on the SD card. The *.dex files (which often ranges between 10-50% of the total size of an app) still remains on /data partition. So does the settings and sqlite databases this apps uses. So even with A2SD the space will become less and less the more apps the user installs



来源:https://stackoverflow.com/questions/3977998/guidance-on-android-file-storage

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