Method of Reading Pictures from sd Card by Android Programming

  • 2021-07-01 08:13:14
  • OfStack

This paper describes the method of reading pictures in sd card by Android. Share it for your reference, as follows:

1. Get permission to read the SD card


<!-- In SDCard Create and delete files permissions in  -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--  To SDCard Write data permission  -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--  From SDCard Read data permission  -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2. Find the directory of the SD card


/**
* Environment.getExternalStorageDirectory() Get: ", mnt/sdcard "   I found it sd Root directory of card 
*/
private String path=Environment.getExternalStorageDirectory()+"client/tile/1.jpg";

3. Get the picture from the path


File mFile=new File(path);
// If the file exists 
if (mFile.exists()) {
  Bitmap bitmap=BitmapFactory.decodeFile(path);
  return bitmap;
}

Note:

Some Available Judgments in the Process of Reading Pictures in sd Card

1. Is the path obtained successfully


/**
* Environment.getExternalStorageState() Is getting the path successful 
*/
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  String path=Environment.getExternalStorageDirectory().getPath();
}

2. Successful acquisition of SD card authority


private String path=Environment.getExternalStorageDirectory()+"client";
File file=new File(path);
if (file.canRead()) {
  Log.v("TAG", " Readable ");
}
if (file.canWrite()) {
  Log.v("TAG", " Writable ");
}

For more readers interested in Android related content, please check the topics on this site: "Summary of SD Card Operation Methods for Android Programming Development", "Introduction and Advanced Tutorial for Android Development", "Summary of Android Resource Operation Skills", "Summary of Android View View Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: