Path optimization:
When the API is not used:
// External storage space path "Sdcard/info.txt" or "Storage/sdcard/info.txt" File file =new file ("Sdcard/info.txt");
Using the API:
// use the API to return a file object whose path is the true path of the SD card File File =new file (Environment.getexternalstoragedirectory (), "info.txt");
Toast Optimization:
When not optimized:
// Create a Toast dialog box /* The first parameter on the context, and activity is originally the subclass of the context, so directly fill this */ /* The third parameter is the continuous display time, with only Length_short (2S) and Length_long (5s) Options */ Toast T= Toast.maketext (This, "login successful", Toast.length_short); // Show Toast dialog box t.show ();
After optimization:
/* *toast.length_short=0*toast.length_long =1*/toast.maketext (This, "Login successful" , Toast.length_short). Show (); // 0 Toast.maketext (This, "login successful", Toast.length_long). Show (); // 1
SD Card Detection Status:
// Media_unknown: SD card not recognized // media_removed: no SD card // MEDIA_UNMOUNTED:SD card exists but not mounted // MEDIA_CHECKING:SD Card is being prepared . // MEDIA_MOUNTED:SD card is already mounted and available
So use:
If the phone is plugged into an SD card and the application has the ability to read and write the SD card, the following statement returns True
If(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) { // Read-write file Operation }else{ Toast.maketext (this, "SD card is not available Yo-Pro", 0). Show ();
Android External storage path, toast optimization problem