开发者

Guidance on Android file storage

I need to write an Android app that would download sounds from the internet for further use.

Where should i store these sou开发者_如何学运维nds? Is Sqlite an option? And what's the best way to load and play these files?

Thanks in advance.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜