日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Storage Options

發(fā)布時間:2025/7/25 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Storage Options 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

閱讀:http://developer.android.com/guide/topics/data/data-storage.html

?

主要類型有:

Shared Preferences
??? 使用鍵值對存儲
Internal Storage
??? 在內(nèi)存存儲私人數(shù)據(jù)。
External Storage
??? 在內(nèi)存存儲共用的數(shù)據(jù)。
SQLite Databases
??? 使用內(nèi)部數(shù)據(jù)庫。
Network Connection
??? 網(wǎng)絡連接來存儲。


?

使用Shared Preferences

可以通過以下方式獲取SharedPreferences:

??? getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
??? getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.

public class Calc extends Activity {public static final String PREFS_NAME = "MyPrefsFile";@Overrideprotected void onCreate(Bundle state){super.onCreate(state);. . .// Restore preferencesSharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);boolean silent = settings.getBoolean("silentMode", false);setSilent(silent);}@Overrideprotected void onStop(){super.onStop();// We need an Editor object to make preference changes.// All objects are from android.context.ContextSharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);SharedPreferences.Editor editor = settings.edit();editor.putBoolean("silentMode", mSilentMode);// Commit the edits! editor.commit();} }

?

使用Internal Storage

You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.

  可以直接在私有的空間里直接存儲文件,其他的程序和用戶都無法訪問它,當程序被卸載的時候,這些文件也會被刪除。

——————————

String FILENAME = "hello_file"; String string = "hello world!";FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close();

MODE_PRIVATE 會創(chuàng)建一個私有的文件。 Other modes available are: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.

——————————

To read a file from internal storage:

  • Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
  • Read bytes from the file with read().
  • Then close the stream with close().
  • Raw的訪問方法:

    If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw.<filename> resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file).

    ?

    Other useful methods

    getFilesDir()
    Gets the absolute path to the filesystem directory where your internal files are saved.
    getDir()
    Creates (or opens an existing) directory within your internal storage space.
    deleteFile()
    Deletes a file saved on the internal storage.
    fileList()
    Returns an array of files currently saved by your application.

    ?

    ?

    使用external storage

    我們可以先檢測一下media的可用性:

    boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; String state = Environment.getExternalStorageState();if (Environment.MEDIA_MOUNTED.equals(state)) {// We can read and write the mediamExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {// We can only read the mediamExternalStorageAvailable = true;mExternalStorageWriteable = false; } else {// Something else is wrong. It may be one of many other states, but all we need// to know is we can neither read nor writemExternalStorageAvailable = mExternalStorageWriteable = false; }

    ——————————

    If you're using API Level 8 or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to receive the root of your application's file directory). This method will create the appropriate directory if necessary. By specifying the type of directory, you ensure that the Android's media scanner will properly categorize your files in the system (for example, ringtones are identified as ringtones and not music). If the user uninstalls your application, this directory and all its contents will be deleted.

    If you're using API Level 7 or lower, use getExternalStorageDirectory(), to open a File representing the root of the external storage. You should then write your data in the following directory:

    /Android/data/<package_name>/files/

    The <package_name> is your Java-style package name, such as "com.example.android.app". If the user's device is running API Level 8 or greater and they uninstall your application, this directory and all its contents will be deleted.

      如果使用API8或者更高級的,可使用getExternalFilesDir()打開一個目錄來存儲文件,在參數(shù)里面可以有DIRECTORY_MUSIC 或者其他的,如果為null,則會選擇屬于本程序的目錄,本程序的目錄在卸載的時候會被刪除掉。

      如果使用API8以下的,則需要使用getExternalStorageDirectory()并且手動選擇目錄。

    ——————————

    如果想要創(chuàng)建能夠共享的文件,程序被刪除后文件依然存在,則可以選擇以下方法:

    In API Level 8 or greater, use getExternalStoragePublicDirectory(), passing it the type of public directory you want, such as DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES, or others. This method will create the appropriate directory if necessary.

    If you're using API Level 7 or lower, use getExternalStorageDirectory() to open a File that represents the root of the external storage, then save your shared files in one of the following directories:

    • Music/ - Media scanner classifies all media found here as user music.
    • Podcasts/ - Media scanner classifies all media found here as a podcast.
    • Ringtones/ - Media scanner classifies all media found here as a ringtone.
    • Alarms/ - Media scanner classifies all media found here as an alarm sound.
    • Notifications/ - Media scanner classifies all media found here as a notification sound.
    • Pictures/ - All photos (excluding those taken with the camera).
    • Movies/ - All movies (excluding those taken with the camcorder).
    • Download/ - Miscellaneous downloads.

    ?

    使用數(shù)據(jù)庫

    使用數(shù)據(jù)庫需要一個繼承SQLiteOpenHelper的類,并覆蓋oncreate方法來創(chuàng)建數(shù)據(jù)庫。

    public class DictionaryOpenHelper extends SQLiteOpenHelper {private static final int DATABASE_VERSION = 2;private static final String DICTIONARY_TABLE_NAME = "dictionary";private static final String DICTIONARY_TABLE_CREATE ="CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +KEY_WORD + " TEXT, " +KEY_DEFINITION + " TEXT);";DictionaryOpenHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL(DICTIONARY_TABLE_CREATE);} }

    想要更好的學習,可以參照官方例子:

    For sample apps that demonstrate how to use SQLite databases in Android, see the Note Pad and Searchable Dictionary applications.

    例子在本地,路徑是:android-sdk\docs\guide\samples

    ?

    ?

    ?

    ?

    ?

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/yutoulck/p/3388490.html

    總結(jié)

    以上是生活随笔為你收集整理的Storage Options的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。