Android reading SDcard on the picture is very easy things, the following example to illustrate the problem.
First of all, there's a img25.jpg on the sdcard that's ready.
The next thing you need to do is to read this image into the app display.
For example, the following effects:
1, first you want to read the SDcard permission in Androidmanifest.xml, add a statement, androidmanifest.xml such as the following:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.sdcardread" Android: Versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" ANDROID:TARGETSD kversion= "/> <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/> <!-- Write data to SDcard permissions--<application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" Android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity Android:nam E= "com.sdcardread.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "android.intent . category. LAUNCHER "/> </intent-filter> </activity> </application></manifest>
2, after Res\values\strings.xml change this app name is called "Picture read". This step can not be done, just for the sake of the program more beautiful.
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "app_name" > Picture read </string> <string name= "action_settings ">Settings</string></resources>
3, second in the res\layout\activity_main.xml to decorate a textview with ID, a moment of information will be written to this textview, the same time to decorate a linear layout with the ID. The image will be added to this linear layout for a while.
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > < TextView android:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:textsize=" 24sp "/> <textview android:layout_width=" Wrap_content "Android:la yout_height= "Wrap_content" android:textsize= "24sp"/> <linearlayout android:id= "@+id/linearlayout1" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizo Ntal "> </linearlayout></linearlayout>
4, the core of the entire program in Mainactivity.java, code such as the following, after obtaining the component, first use Environment.getexternalstoragestate (). Equals (Environment.media_ mounted); infer whether SDcard exists. Then use Environment.getexternalstoragedirectory (). GetAbsolutePath (); Gets the absolute path of the sdcard for read by the Java file class.
Finally, a ImageView object is created and loaded into the linear layout linearLayout1.
Package Com.sdcardread;import Java.io.file;import Android.os.bundle;import android.os.environment;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.textview;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;public Class Mainactivity extends Activity {private TextView textview1;private linearlayout linearLayout1; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView1 = (TextView) Findviewbyid (r.id.textview1); linearLayout1 = (linearlayout) Findviewbyid (R.ID.LINEARLAYOUT1); Boolean issdcardexist = Environment.getexternalstoragestate (). Equals (environment.media_mounted);//Infer whether sdcard exists if ( issdcardexist) {String Sdpath = Environment.getexternalstoragedirectory (). GetAbsolutePath ();// Gets the root path of the SDcard textview1.settext ("SD card is present. Below is the img25.jpg! under SDcard. "); String filepath = sdpath + file.separator + "img25.jpg"; File File = new file (filepath); ImageView ImageView = new ImageView (this);//Create a ImageView object if (file.exists ()) {Bitmap BM = Bitmapfactory.decodefile (filepath);//Display the picture in ImageView Imageview.setimagebitmap (BM); Linearlayout1.addview ( ImageView);}} else {textview1.settext ("SD card does not exist!) ");}}}
"Android" read the picture on the SDcard