Android sdcard path access methods: androidsdcard
In previous Android (versions earlier than Android 4.1), the SDcard path is represented by "/sdcard" or "/mnt/sdcard", while in JellyBean (Android 4.1) the system changes to "/storage/sdcard0", and there will be multiple sdcards. To maintain compatibility with the previous code, the SDcard path is mapped to Link. To make the code more robust and compatible with later Android versions and new devices, the sdcard system automatically generates a file directory for storing specific content after Android, in this way, you can use some specific functions to obtain the corresponding directory.
Today, we will briefly introduce several methods to access SDcard paths:
The image Path obtained in this demonstration is as follows:
The complete path is "/storage/emulated/0/Download/tianxingjiuge.jpg"
Below we will introduce the loading methods by loading the "/storage/emulated/0/Download/tianxingjiuge.jpg" image of the local machine to the ImageView demonstration in the APP:
There is a Button at the top of the layout file, which is imagView. The java Implementation Code is as follows:
1 public class MainActivity extends AppCompatActivity {2 ImageView iv; 3 @ Override 4 protected void onCreate (Bundle savedInstanceState) {5 super. onCreate (savedInstanceState); 6 setContentView (R. layout. activity_main); 7 iv = (ImageView) findViewById (R. id. iv); 8} 9 public void load (View view) {10 // first: directly reference all directories (used before 4.1, not recommended later) 11 // Bitmap bm = BitmapFactory. decodeFile ("/storage/emulated/0/Download/tianxingjiuge.jpg"); 12 // The second type:/storage/emulated/0. getExternalStorageDirectory () 13 // Bitmap bm = BitmapFactory. decodeFile (Environment. getExternalStorageDirectory () + 14 // "/Download/tianxingjiuge.jpg"); 15 // The third method is Environment. the getExternalStoragePublicDirectory (String type) function calls different types of file directories 16 Bitmap bm = BitmapFactory. decodeFile (Environment. getExternalStoragePublicDirectory17 (Environment. DIRECTORY_DOWNLOADS) + "/tianxingjiuge.jpg"); 18 iv. setImageBitmap (bm); 19} 20}
The third method uses the Environment. getExternalStoragePublicDirectory (String type) function. This function can return specific types of directories. Currently, the following types are supported:
1 • DIRECTORY_ALARMS // alert tone 2 • DIRECTORY_DCIM // picture taken by the camera and Video 3 • DIRECTORY_DOWNLOADS // download file save 4 • DIRECTORY_MOVIES // Save the movie, for example, movie 5 downloaded through google play: DIRECTORY_MUSIC // music saved 6 • directory_configurications // notification music saved 7 • DIRECTORY_PICTURES // downloaded picture 8 • DIRECTORY_PODCASTS // used to save podcast (blog) audio file 9 • DIRECTORY_RINGTONES // Save the ringtone
This image is saved in the Download directory, so Environment. DIRECTORY_DOWNLOADS is used.