"Android" read the picture on the SDcard

Source: Internet
Author: User

Android reads the picture on the sdcard is very simple thing, below uses an example to illustrate this question.

First of all, there's a img25.jpg on the sdcard that's ready.


Below, what needs to be done is to read this image into the app display. Achieve the following effects:


1, first you want to apply to read SDcard permission in Androidmanifest.xml, after adding a statement, androidmanifest.xml as follows:

<?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 A ndroid:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" Android.permissio N.write_external_storage "/> <!--write data to SDcard right--<application android:allowbackup=" true "a        ndroid:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" >            <activity android:name= "com.sdcardread.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> & Lt;category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> &LT;/ACTIVITY&G    T </application></manifest> 
2, after res\values\strings.xml modify this app name called "Picture read", this step can not do, just for 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 prompt information will be written to this textview, at the same time 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 as follows, get the component, first use Environment.getexternalstoragestate (). Equals (Environment.media_ mounted); Determine if 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);//Determine if sdcard exists if ( issdcardexist) {String Sdpath = Environment.getexternalstoragedirectory (). GetAbsolutePath ();// Gets the root path of the SDcard textview1.settext ("SD card is present. The following 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

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.