|
首先內(nèi)部存儲(chǔ)路徑為/data/data/youPackageName/,下面講解的各路徑都是基于你自己的應(yīng)用的內(nèi)部存儲(chǔ)路徑下。所有內(nèi)部存儲(chǔ)中保存的文件在用戶卸載應(yīng)用的時(shí)候會(huì)被刪除。 有了數(shù)據(jù)存儲(chǔ) API,您可以使用內(nèi)部存儲(chǔ)器存儲(chǔ)數(shù)據(jù)。信息可以是私有的,您可以有選擇地讓其他應(yīng)用程序?qū)χ哂凶x或?qū)懙脑L問(wèn)權(quán)限。本節(jié)介紹這個(gè)存儲(chǔ)私有數(shù)據(jù)的 API,它使用 android.content.Context.openFileInput、openFileOutput 和 getCacheDir() 來(lái)高速緩存數(shù)據(jù),而不是永久地存儲(chǔ)。 清單 20 中的代碼片段展示了如何從內(nèi)部私有存儲(chǔ)器讀取數(shù)據(jù)。使得存儲(chǔ)器為私有的方法是對(duì) openFileOutput() 使用MODE_PRIVATE。 清單 20. 從本地私有存儲(chǔ)器讀取數(shù)據(jù) /** * Writes content to internal storage making the content private to * the application. The method can be easily changed to take the MODE * as argument and let the caller dictate the visibility: * MODE_PRIVATE, MODE_WORLD_WRITEABLE, MODE_WORLD_READABLE, etc. * * @param filename - the name of the file to create * @param content - the content to write */ public void writeInternalStoragePrivate( String filename, byte[] content) { try { //MODE_PRIVATE creates/replaces a file and makes // it private to your application. Other modes: // MODE_WORLD_WRITEABLE // MODE_WORLD_READABLE // MODE_APPEND FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE); fos.write(content); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 清單 21 中的代碼片段展示了如何從內(nèi)部私有存儲(chǔ)器讀取數(shù)據(jù);注意 openFileInput() 的使用。 清單 21. 從內(nèi)部私有存儲(chǔ)器讀取數(shù)據(jù) /** * Reads a file from internal storage * @param filename the file to read from * @return the file content */ public byte[] readInternalStoragePrivate(String filename) { int len = 1024; byte[] buffer = new byte[len]; try { FileInputStream fis = openFileInput(filename); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int nrb = fis.read(buffer, 0, len); // read up to len bytes while (nrb != -1) { baos.write(buffer, 0, nrb); nrb = fis.read(buffer, 0, len); } buffer = baos.toByteArray(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } 清單 22 展示了如何從內(nèi)部私有存儲(chǔ)器刪除數(shù)據(jù)。 清單 22. 從本地私有存儲(chǔ)器刪除數(shù)據(jù) /** * Delete internal private file * @param filename - the filename to delete */ public void deleteInternalStoragePrivate(String filename) { File file = getFileStreamPath(filename); if (file != null) { file.delete(); } } 現(xiàn)在可以來(lái)看為公共數(shù)據(jù)使用外部存儲(chǔ)器了。 回頁(yè)首 為公共數(shù)據(jù)使用設(shè)備的外部存儲(chǔ)器 有了數(shù)據(jù)存儲(chǔ) API,您可以使用外部存儲(chǔ)器存儲(chǔ)數(shù)據(jù)。信息可以是私有的,您可以有選擇地讓其他應(yīng)用程序?qū)χ哂凶x或?qū)懙脑L問(wèn)權(quán)限。本節(jié)您將對(duì)此 API 進(jìn)行編程,以便使用包括getExternalStorageState()、getExternalFilesDir()、getExternalStorageDirectory() 和getExternalStoragePublicDirectory() 在內(nèi)的很多 API 來(lái)存儲(chǔ)公共數(shù)據(jù)。您為公共數(shù)據(jù)使用下面的路徑:/Android/data/<package_name>/files/。 在使用外部存儲(chǔ)器之前,必須看看它是否可用,是否可寫。下面兩個(gè)代碼片段展示了測(cè)試這些條件的幫助器方法。清單 23 測(cè)試外部存儲(chǔ)器是否可用。 清單 23. 測(cè)試外部存儲(chǔ)器是否可用 /** * Helper Method to Test if external Storage is Available */ public boolean isExternalStorageAvailable() { boolean state = false; String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { state = true; } return state; } 清單 24 測(cè)試外部存儲(chǔ)器是否只可讀。 清單 24. 測(cè)試外部存儲(chǔ)器是否只可讀 /** * Helper Method to Test if external Storage is read only */ public boolean isExternalStorageReadOnly() { boolean state = false; String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { state = true; } return state; } 清單 25 展示了如何寫到外部存儲(chǔ)器,以存儲(chǔ)公共數(shù)據(jù)。 清單 25. 寫到外部?jī)?nèi)存 /** * Write to external public directory * @param filename - the filename to write to * @param content - the content to write */ public void writeToExternalStoragePublic(String filename, byte[] content) { // API Level 7 or lower, use getExternalStorageDirectory() // to open a File that represents the root of the external // storage, but writing to root is not recommended, and instead // application should write to application-specific directory, as shown below. String packageName = this.getPackageName(); String path = "/Android/data/" + packageName + "/files/"; if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) { try { File file = new File(path, filename); file.mkdirs(); FileOutputStream fos = new FileOutputStream(file); fos.write(content); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 清單 26 展示了如何從外部存儲(chǔ)器讀取數(shù)據(jù)。 清單 26. 從外部?jī)?nèi)存讀取數(shù)據(jù) /** * Reads a file from internal storage * @param filename - the filename to read from * @return the file contents */ public byte[] readExternallStoragePublic(String filename) { int len = 1024; byte[] buffer = new byte[len]; String packageName = this.getPackageName(); String path = "/Android/data/" + packageName + "/files/"; if (!isExternalStorageReadOnly()) { try { File file = new File(path, filename); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int nrb = fis.read(buffer, 0, len); //read up to len bytes while (nrb != -1) { baos.write(buffer, 0, nrb); nrb = fis.read(buffer, 0, len); } buffer = baos.toByteArray(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return buffer; } 清單 27 中的代碼片段展示了如何從外部?jī)?nèi)存刪除文件。 清單 27. 從外部?jī)?nèi)存刪除文件 /** * Delete external public file * @param filename - the filename to write to */ void deleteExternalStoragePublicFile(String filename) { String packageName = this.getPackageName(); String path = "/Android/data/" + packageName + "/files/"+filename; File file = new File(path, filename); if (file != null) { file.delete(); } } 處理外部存儲(chǔ)器需要特殊的權(quán)限 WRITE_EXTERNAL_STORAGE,它通過(guò) AndroidManifest.xml 請(qǐng)求得到(參見 清單 28)。 清單 28. WRITE_EXTERNAL_STORAGE <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 外部存儲(chǔ) API 通過(guò)根據(jù)文件類型(比如 Pictures、Ringtones)將文件存儲(chǔ)在預(yù)先確定的目錄中,允許您公共地存儲(chǔ)文件。本文沒(méi)有介紹這種方法,但是您應(yīng)該熟悉它。此外,記住外部存儲(chǔ)器中的文件任何時(shí)候都可能消失。 回頁(yè)首 相關(guān)的方法 如果您具有不需要長(zhǎng)期永久保存的臨時(shí)文件,那么可以將這些文件存儲(chǔ)在高速緩存中。高速緩存是一種特殊的內(nèi)存,可以用于存儲(chǔ)中小型數(shù)據(jù)(少于兆字節(jié)),但是您一定要知道,取決于有多少內(nèi)存可用,高速緩存的內(nèi)容任何時(shí)候都可能被清除。 清單 29 展示了一個(gè)幫助器方法,它返回到內(nèi)部?jī)?nèi)存中高速緩存的路徑。 清單 29. 檢索到內(nèi)部?jī)?nèi)存高速緩存的路徑 /** * Helper method to retrieve the absolute path to the application * specific internal cache directory on the file system. These files * will be ones that get deleted when the application is uninstalled or when * the device runs low on storage. There is no guarantee when these * files will be deleted. * * Note: This uses a Level 8+ API. * * @return the absolute path to the application specific cache * directory */ public String getInternalCacheDirectory() { String cacheDirPath = null; File cacheDir = getCacheDir(); if (cacheDir != null) { cacheDirPath = cacheDir.getPath(); } return cacheDirPath; } 清單 30 展示了一個(gè)幫助器方法,它返回到外部?jī)?nèi)存中高速緩存的路徑。 清單 30. 檢索到外部?jī)?nèi)存高速緩存的路徑 /** * Helper method to retrieve the absolute path to the application * specific external cache directory on the file system. These files * will be ones that get deleted when the application is uninstalled or when * the device runs low on storage. There is no guarantee when these * files will be deleted. * * Note: This uses a Level 8+ API. * * @return the absolute path to the application specific cache * directory */ public String getExternalCacheDirectory() { String extCacheDirPath = null; File cacheDir = getExternalCacheDir(); if (cacheDir != null) { extCacheDirPath = cacheDir.getPath(); } return extCacheDirPath; } 通過(guò)使用示例應(yīng)用程序,您現(xiàn)在應(yīng)該很好地理解了如何為公共數(shù)據(jù)使用設(shè)備的外部存儲(chǔ)器。 |
|
|