Android應用儲存直接選取方案的設計

來源:互聯網
上載者:User

標籤:sdc   sam   exception   except   androi   img   布爾   分享   尋找   

1方案設計
  在各種應用設計的需求裡,有可能涉及檔案的儲存路徑的選擇問題。本文介紹Android應用中涉及允許選擇SD卡和手機儲存,初步設想的方案:(1)在手機安裝SD卡的情況下,優先選擇儲存在SD卡中,當使用者卸載SD卡、拔出SD卡以及使用者在設定中選擇使用手機儲存時,將選擇儲存在手機儲存中;(2)在手機未安裝SD卡的情況下,將儲存在手機儲存中,當使用者安裝入SD卡後,將主動選擇儲存在SD卡中。軟體流程如所示。

  具體的路徑如下:
SD卡:/storage/603E-5ED9/Movies/RecordVideo/
手機儲存:/storage/emulated/0/Movies/RecordVideo/
2.實施方案
1)擷取使用者佈建中選擇的路徑
  本應用建立了SettingFragment,並將路徑選項用ListPreference執行個體化,在設定路徑時,供使用者在設定介面選擇相應的路徑。
  程式擷取使用者在設定中選擇的路徑,能通過擷取此時設定介面中路徑的選擇項來判斷此時的路徑。由於我們提供給使用者的只有2個選項:儲存在手機記憶體中和儲存在SD卡中。因此,在程式中使用布爾變數來判斷使用者是否選擇使用SD卡儲存視頻。具體代碼如下:

        SharedPreferences preferences = getSharedPreferences("recordscreensettings", MODE_PRIVATE);        String typeView=preferences.getString(Config.ViewType, "0");
        String location=preferences.getString(Config.Location, "0");        Log.v(tag, "location is "+location);        if (location.equals("0")) {            isUseSDcard=true;            Log.v(tag, "isUseSDcard is "+isUseSDcard);        }else  {            isUseSDcard=false;            Log.v(tag, "isUseSDcard is "+isUseSDcard);        }

  可以通過判斷isUseSDcard的值擷取使用者當前在設定中選擇的路徑。
2)擷取手機中當前SD卡的掛載情況
  在擷取使用者在當前設定中選擇的儲存路徑的同時,必須確定的是當前手機的SD卡是否掛載。有下面的情況是有可能存在的:手機中沒有安裝SD卡,手機中安裝的SD卡沒有識別和使用者將SD卡卸載了。針對這種情況,我們在選擇路徑時應該判斷當前SD卡的掛載情況。我們建立一個StorageUtils類管理應用的儲存路徑,寫一個判斷當前SD卡的掛載情況的方法。代碼如下:

   public static boolean isSDCardExist(Context context, Object storageVolume) {        boolean sdcardExist = isVolumeMounted(storageVolume);        DebugLog.d(TAG, "isSDCardExist(): " + sdcardExist);        return sdcardExist;    }     private static boolean isVolumeMounted(Object storageVolume) {        boolean mountble = false;        String storagePath = null;        String state = null;        try {            if (null == storageVolume) {                Log.d(TAG, "null == storageVolume failed in isVolumeRemoveble");            }            if (null == mStorageManager) {                mStorageManager = mContext.getSystemService(Context.STORAGE_SERVICE);            }            storagePath = (String) storageVolume.getClass().getMethod("getPath").invoke(storageVolume);            if (null == storagePath || 0 == storagePath.length()) {                return false;            }            state = (String) mStorageManager.getClass().getMethod("getVolumeState", String.class)                    .invoke(mStorageManager, storagePath);            if (Environment.MEDIA_MOUNTED.equalsIgnoreCase(state)) {                mountble = true;            }        } catch (Exception e) {            Log.e(TAG, "Storage_Standard.isVolumeRemoveble()", e);        }        return mountble;    }

  通過判斷isSDCardExist方法返回的布爾值判斷此時SD卡是否掛載。
3)對擷取儲存路徑進行有效選擇
  對於路徑的選擇該應用只提供了2種選擇方式。當選擇SD卡成為視頻儲存的路徑時,必須滿足2個條件:

(1)使用者在設定介面選擇使用SD卡作為儲存路徑;

(2)使用SD卡儲存視頻時,SD卡掛載。通過上述分析,在擷取視頻儲存路徑時,應該對儲存路徑進行有效選擇,也就是直接選取正確且路徑可用。

       if (isHaveSDcard&&isUseSDcard) {            String pathSDStorage=storageUtils.getStorageDirectory(SdCardType.SDStorage);            Log.v(tag, "pathSDStorage is "+pathSDStorage);            pathstring=pathSDStorage;        }else {            String pathInternelStorage=storageUtils.getStorageDirectory(SdCardType.InternelStorage);            Log.v(tag, "pathInternelStorage is "+pathInternelStorage);            Log.v(tag, "the system of storage is"+Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).toString());            pathstring=pathInternelStorage;        }

4)建立視頻的檔案名稱
  及時建立的檔案應該給檔案取一個合適的名稱,本應用是即時軟體,為了方便使用者區別儲存的檔案,我們採用組建檔案時的時間有關名稱。名稱格式如下:年-月-日_時_分_秒。

        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd_hh_mm_ss");        String date = "/myscreen_" + sDateFormat.format(new java.util.Date());    

5)建立完整的視頻儲存路徑
  通過上述的步驟,我們就擷取到了檔案的儲存路徑以及檔案的名稱,接下來需要完成建立完整的檔案儲存體路徑。為了方便使用者在檔案夾目錄下尋找,不論在SD卡還是手機中儲存錄檔案,我們將儲存的檔案放置在../Movies/RecordVideo/(根據自己的應用取名)的檔案夾下。具體代碼如下。

        sampleDir=new File(pathstring+File.separator+"Movies"+ File.separator + "RecordVideo/");        if (!sampleDir.exists()) {            sampleDir.mkdirs();            Log.v(tag, "建立檔案夾"+sampleDir.mkdirs());        }        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd_hh_mm_ss");        String date = "/myscreen_" + sDateFormat.format(new java.util.Date());        Log.v(tag, "date=" + date);        mVecordFile = new File(sampleDir.toString() + date + ".mp4");        Log.v(tag, mVecordFile.toString());

3.總結
  我們在擷取儲存路徑時,應該充分考慮使用者在設定中的直接選取以及儲存路徑是否存在的情況。

Android應用儲存直接選取方案的設計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.