Design of Storage path selection scheme for Android application

Source: Internet
Author: User

1 Scheme design
In a variety of application design requirements, it is possible to refer to the file storage path selection problem. This article describes the Android application involved in allowing the selection of SD card and cell phone storage, the initial scenario: (1) in the case of the phone installed SD card, the preference is stored in the SD card, when the user uninstalls the SD card, unplug the SD card and the user in the settings to choose to use the phone storage, will choose to store in the phone storage ; (2) If the SD card is not installed on the phone, it will be stored in the phone storage, when the user installs the SD card, the active selection is stored in the SD card. The software flow is as shown.

The specific path is as follows:
SD Card:/storage/603e-5ed9/movies/recordvideo/
Phone Storage:/storage/emulated/0/movies/recordvideo/
2. Implementation of the programme
1) Get the path selected in the user settings
The application creates a settingfragment and instantiates the path option with Listpreference, where the user selects the appropriate path in the Setup interface when the path is set.
The program obtains the path that the user chooses in the setting, can judge the path at this time by getting the selection of the path in the setting interface at this time. Since we provide users with only 2 options: stored in the phone memory and stored in the SD card. Therefore, Boolean variables are used in the program to determine whether the user chooses to store the video using an SD card. The specific code is as follows:

 sharedpreferences preferences = getsharedpreferences ("recordscreensettings" , MOD        E_private); String typeview  =preferences.getstring (Config.viewtype, "0"); 
 String location=preferences.getstring (config.location, "0"  location is "+location);  if  (Location.equals ("0"  =true  ;        LOG.V (Tag,  "Isusesdcard is" +isusesdcard);  else   {isusesdcard  =false  ;        LOG.V (Tag,  "Isusesdcard is" +isusesdcard); }

You can get the path that the user is currently selecting in the settings by judging the value of Isusesdcard.
2) Get the current SD card mount status on the phone
You must determine whether the current phone's SD card is mounted while getting the storage path that the user has selected in the current settings. There may be situations where the SD card is not installed in the phone, the SD card installed in the phone is not recognized and the user has uninstalled the SD card. In this case, we should determine the current mount of the SD card when selecting the path. We create a storage path for the Storageutils class management application and write a method to determine the current mount of the SD card. The code is as follows:

 Public Static Booleanissdcardexist (Context context, Object Storagevolume) {BooleanSdcardexist =isvolumemounted (Storagevolume); DEBUGLOG.D (TAG,"Issdcardexist ():" +sdcardexist); returnsdcardexist; }Private Static Booleanisvolumemounted (Object storagevolume) {BooleanMountble =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); }        returnmountble; }

Determines whether the SD card is mounted at this time by judging the Boolean value returned by the Issdcardexist method.
3) Effective selection of access to the storage path
The app provides only 2 options for selecting the path. When choosing an SD card as the path to the video store, 2 conditions must be met:

(1) The user chooses to use the SD card as the storage path in the setting interface;

(2) SD card is mounted when using SD card to store video. Through the above analysis, in obtaining the video storage path, the storage path should be effectively selected, that is, the path is selected correctly and the path is available.

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) Create a file name for the video
The timely creation of the file should give the file a proper name, the application is instant software, in order to facilitate the user to distinguish between stored files, we take the time to generate files about the name. The name format is as follows: year-month-day _ _ minutes _ seconds.

        New SimpleDateFormat ("Yyyy-mm-dd_hh_mm_ss");         = "/myscreen_" + Sdateformat.format (new  java.util.Date ());    

5) Create a complete video storage path
With the above steps, we get the file's storage path and the file name, then we need to complete the creation of the complete file storage path. In order to make it easier for users to find files in the folder directory, we put the stored files on the SD card or on the phone. /movies/recordvideo/(named according to your app) under the folder. The specific code is as follows.

 sampledir=new  File (pathstring+file.separator+ "Movies" + File.separator + "        recordvideo/");  if  (! create folder +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. Summary
When we get the storage path, we should take into account the user's path selection in the settings and the existence of the storage path.

Design of Storage path selection scheme for Android application

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.