徹底了解android中的內(nèi)部存儲與外部存儲

轉(zhuǎn)自:http://www.cnblogs.com/jingmo0319/p/5586559.html
公司網(wǎng)絡(luò)經(jīng)常打不開cnblog所以復(fù)制一下。


我們先來考慮這樣一個問題:

打開手機設(shè)置,選擇應(yīng)用管理,選擇任意一個App,然后你會看到兩個按鈕,一個是清除緩存,另一個是清除數(shù)據(jù),那么當(dāng)我們點擊清除緩存的時候清除的是哪里的數(shù)據(jù)?當(dāng)我們點擊清除數(shù)據(jù)的時候又是清除的哪里的數(shù)據(jù)?讀完本文相信你會有答案。

在android開發(fā)中我們常常聽到這樣幾個概念,內(nèi)存,內(nèi)部存儲,外部存儲,很多人常常將這三個東西搞混,那么我們今天就先來詳細說說這三個東西是怎么回事?

內(nèi)存,我們在英文中稱作memory,內(nèi)部存儲,我們稱為InternalStorage,外部存儲我們稱為ExternalStorage,這在英文中本不會產(chǎn)生歧義,但是當(dāng)我們翻譯為中文之后,前兩個都簡稱為內(nèi)存,于是,混了。

那么究竟什么是內(nèi)部存儲什么是外部存儲呢?

首先我們打開DDMS,有一個File Explorer,如下:

10541376.png

這里有三個文件夾需要我們重視,一個是data,一個是mnt,一個是storage,我們下面就詳細說說這三個文件夾。

1. 內(nèi)部存儲

data文件夾就是我們常說的內(nèi)部存儲,當(dāng)我們打開data文件夾之后(沒有root的手機不能打開該文件夾),里邊有兩個文件夾值得我們關(guān)注,如下:

003821320.png

一個文件夾是app文件夾,還有一個文件夾就是data文件夾,app文件夾里存放著我們所有安裝的app的apk文件,其實,當(dāng)我們調(diào)試一個app的時候,可以看到控制臺輸出的內(nèi)容,有一項是uploading .....就是上傳我們的apk到這個文件夾,上傳成功之后才開始安裝。另一個重要的文件夾就是data文件夾了,這個文件夾里邊都是一些包名,打開這些包名之后我們會看到這樣的一些文件:

1.data/data/包名/shared_prefs
2.data/data/包名/databases
3.data/data/包名/files
4.data/data/包名/cache

如果打開過data文件,應(yīng)該都知道這些文件夾是干什么用的,我們在使用sharedPreferenced的時候,將數(shù)據(jù)持久化存儲于本地,其實就是存在這個文件中的xml文件里,我們App里邊的數(shù)據(jù)庫文件就存儲于databases文件夾中,還有我們的普通數(shù)據(jù)存儲在files中,緩存文件存儲在cache文件夾中,存儲在這里的文件我們都稱之為內(nèi)部存儲。

2.外部存儲

外部存儲才是我們平時操作最多的,外部存儲一般就是我們上面看到的storage文件夾,當(dāng)然也有可能是mnt文件夾,這個不同廠家有可能不一樣。
一般來說,在storage文件夾中有一個sdcard文件夾,這個文件夾中的文件又分為兩類,一類是公有目錄,還有一類是私有目錄,其中的公有目錄有九大類,比如DCIM、DOWNLOAD等這種系統(tǒng)為我們創(chuàng)建的文件夾,私有目錄就是Android這個文件夾,這個文件夾打開之后里邊有一個data文件夾,打開這個data文件夾,里邊有許多包名組成的文件夾。
說到這里,我想大家應(yīng)該已經(jīng)可以分清楚什么是內(nèi)部存儲什么是外部存儲了吧?好,分清楚之后我們就要看看怎么來操作內(nèi)部存儲和外部存儲了。

3.操作存儲空間

首先,經(jīng)過上面的分析,大家已經(jīng)明白了,什么是內(nèi)部存儲,什么是外部存儲,以及這兩種存儲方式分別存儲在什么位置,一般來說,我們不會自己去操作內(nèi)部存儲空間,沒有root權(quán)限的話,我們也沒法操作內(nèi)部存儲空間,事實上內(nèi)部存儲主要是由系統(tǒng)來維護的。不過在代碼中我們是可以訪問到這個文件夾的。由于內(nèi)部存儲空間有限,在開發(fā)中我們一般都是操作外部存儲空間,Google官方建議我們App的數(shù)據(jù)應(yīng)該存儲在外部存儲的私有目錄中該App的包名下,這樣當(dāng)用戶卸載掉App之后,相關(guān)的數(shù)據(jù)會一并刪除,如果你直接在/storage/sdcard目錄下創(chuàng)建了一個應(yīng)用的文件夾,那么當(dāng)你刪除應(yīng)用的時候,這個文件夾就不會被刪除。

經(jīng)過以上的介紹,我們可以總結(jié)出下面一個表格:

003821321.png

大家看到,有包名的路徑我們都是調(diào)用Context中的方法來獲得,沒有包名的路徑,我們直接調(diào)用Environment中的方法獲得,那么其中有兩個方法需要傳入一個String類型的參數(shù),這個參數(shù)我們使用了Environment中的常量,參數(shù)的意思是我們要訪問這個路徑下的哪個文件夾,比如getExternalFilesDir方法,我們看看它的源碼:

/**
     *
     * @param type The type of files directory to return.  May be null for
     * the root of the files directory or one of
     * the following Environment constants for a subdirectory:
     * {@link android.os.Environment#DIRECTORY_MUSIC},
     * {@link android.os.Environment#DIRECTORY_PODCASTS},
     * {@link android.os.Environment#DIRECTORY_RINGTONES},
     * {@link android.os.Environment#DIRECTORY_ALARMS},
     * {@link android.os.Environment#DIRECTORY_NOTIFICATIONS},
     * {@link android.os.Environment#DIRECTORY_PICTURES}, or
     * {@link android.os.Environment#DIRECTORY_MOVIES}.
     *
     * @return The path of the directory holding application files
     * on external storage.  Returns null if external storage is not currently
     * mounted so it could not ensure the path exists; you will need to call
     * this method again when it is available.
     *
     * @see #getFilesDir
     * @see android.os.Environment#getExternalStoragePublicDirectory
     */
    @Nullable
    public abstract File getExternalFilesDir(@Nullable String type);

它的注釋非常多,我這里只列出其中一部分,我們看到,我們可以訪問files文件夾下的Music文件夾、Movies文件夾等等好幾種。
說到這里,我想大家對內(nèi)部存儲、外部存儲該有了一個清晰的認識了吧。我們在開發(fā)中,不建議往內(nèi)部存儲中寫太多的數(shù)據(jù),畢竟空間有限。外部存儲在使用的時候最好能夠?qū)⑽募娣旁谒接心夸浵拢@樣有利于系統(tǒng)維護,也避免用戶的反感。

現(xiàn)在我們再來看看我們一開始提出的問題,當(dāng)我們點擊清除數(shù)據(jù)的時候清除的是哪里的數(shù)據(jù)呢?毫無疑問,當(dāng)然是內(nèi)部存儲目錄中相應(yīng)的files和cache文件夾中的文件和外部存儲中相應(yīng)的files和cache文件夾中的文件,至于這些文件夾的路徑我想你應(yīng)該已經(jīng)明白了。

好了,最后再送給大家一個文件操作工具類:

public class SDCardHelper {

    // 判斷SD卡是否被掛載
    public static boolean isSDCardMounted() {
        // return Environment.getExternalStorageState().equals("mounted");
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }

    // 獲取SD卡的根目錄
    public static String getSDCardBaseDir() {
        if (isSDCardMounted()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return null;
    }

    // 獲取SD卡的完整空間大小,返回MB
    public static long getSDCardSize() {
        if (isSDCardMounted()) {
            StatFs fs = new StatFs(getSDCardBaseDir());
            long count = fs.getBlockCountLong();
            long size = fs.getBlockSizeLong();
            return count * size / 1024 / 1024;
        }
        return 0;
    }

    // 獲取SD卡的剩余空間大小
    public static long getSDCardFreeSize() {
        if (isSDCardMounted()) {
            StatFs fs = new StatFs(getSDCardBaseDir());
            long count = fs.getFreeBlocksLong();
            long size = fs.getBlockSizeLong();
            return count * size / 1024 / 1024;
        }
        return 0;
    }

    // 獲取SD卡的可用空間大小
    public static long getSDCardAvailableSize() {
        if (isSDCardMounted()) {
            StatFs fs = new StatFs(getSDCardBaseDir());
            long count = fs.getAvailableBlocksLong();
            long size = fs.getBlockSizeLong();
            return count * size / 1024 / 1024;
        }
        return 0;
    }

    // 往SD卡的公有目錄下保存文件
    public static boolean saveFileToSDCardPublicDir(byte[] data, String type,
            String fileName) {
        BufferedOutputStream bos = null;
        if (isSDCardMounted()) {
            File file = Environment.getExternalStoragePublicDirectory(type);
            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(
                        file, fileName)));
                bos.write(data);
                bos.flush();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    // 往SD卡的自定義目錄下保存文件
    public static boolean saveFileToSDCardCustomDir(byte[] data, String dir,
            String fileName) {
        BufferedOutputStream bos = null;
        if (isSDCardMounted()) {
            File file = new File(getSDCardBaseDir() + File.separator + dir);
            if (!file.exists()) {
                file.mkdirs();// 遞歸創(chuàng)建自定義目錄
            }
            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(
                        file, fileName)));
                bos.write(data);
                bos.flush();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    // 往SD卡的私有Files目錄下保存文件
    public static boolean saveFileToSDCardPrivateFilesDir(byte[] data,
            String type, String fileName, Context context) {
        BufferedOutputStream bos = null;
        if (isSDCardMounted()) {
            File file = context.getExternalFilesDir(type);
            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(
                        file, fileName)));
                bos.write(data);
                bos.flush();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    // 往SD卡的私有Cache目錄下保存文件
    public static boolean saveFileToSDCardPrivateCacheDir(byte[] data,
            String fileName, Context context) {
        BufferedOutputStream bos = null;
        if (isSDCardMounted()) {
            File file = context.getExternalCacheDir();
            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(
                        file, fileName)));
                bos.write(data);
                bos.flush();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    // 保存bitmap圖片到SDCard的私有Cache目錄
    public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bitmap,
            String fileName, Context context) {
        if (isSDCardMounted()) {
            BufferedOutputStream bos = null;
            // 獲取私有的Cache緩存目錄
            File file = context.getExternalCacheDir();

            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(
                        file, fileName)));
                if (fileName != null
                        && (fileName.contains(".png") || fileName
                                .contains(".PNG"))) {
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
                } else {
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                }
                bos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return true;
        } else {
            return false;
        }
    }

    // 從SD卡獲取文件
    public static byte[] loadFileFromSDCard(String fileDir) {
        BufferedInputStream bis = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            bis = new BufferedInputStream(
                    new FileInputStream(new File(fileDir)));
            byte[] buffer = new byte[8 * 1024];
            int c = 0;
            while ((c = bis.read(buffer)) != -1) {
                baos.write(buffer, 0, c);
                baos.flush();
            }
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                baos.close();
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    // 從SDCard中尋找指定目錄下的文件,返回Bitmap
    public Bitmap loadBitmapFromSDCard(String filePath) {
        byte[] data = loadFileFromSDCard(filePath);
        if (data != null) {
            Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            if (bm != null) {
                return bm;
            }
        }
        return null;
    }

    // 獲取SD卡公有目錄的路徑
    public static String getSDCardPublicDir(String type) {
        return Environment.getExternalStoragePublicDirectory(type).toString();
    }

    // 獲取SD卡私有Cache目錄的路徑
    public static String getSDCardPrivateCacheDir(Context context) {
        return context.getExternalCacheDir().getAbsolutePath();
    }

    // 獲取SD卡私有Files目錄的路徑
    public static String getSDCardPrivateFilesDir(Context context, String type) {
        return context.getExternalFilesDir(type).getAbsolutePath();
    }

    public static boolean isFileExist(String filePath) {
        File file = new File(filePath);
        return file.isFile();
    }

    // 從sdcard中刪除文件
    public static boolean removeFileFromSDCard(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            try {
                file.delete();
                return true;
            } catch (Exception e) {
                return false;
            }
        } else {
            return false;
        }
    }
}

本文相關(guān)筆記和源碼下載http://download.csdn.net/detail/u012702547/9348985

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容