In this section, we do a very simple demo that allows you to read an image from an SD card and display it in the app.
In three steps:
1. Set permissions.
Add three lines of permission information to the manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2. Configure the MXL view.
can refer to:
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Fill_parent"android:layout_height="Fill_parent" Android:orientation="vertical" > <ImageViewandroid:id= "@+id/myview"android:layout_width="Fill_ Parent "android:layout_height=" fill_parent " /> </linearlayout>
3.Java Code Authoring section.
Roughly two steps: Get the picture file, show the picture
(See Code, notes for detailed procedures)
PackageCom.example.showimage;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.BitmapFactory;ImportAndroid.os.Bundle;ImportAndroid.os.Environment;ImportAndroid.widget.ImageView; Public class mainactivity extends Activity { PrivateImageView MyView =NULL;@Override Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); MyView = (ImageView) Findviewbyid (R.id.myview); String path = environment.getexternalstoragedirectory () +"/";//get the SD card default pathString name = path +"Image.jpg";//get the image File ' s path+namebitmapfactory.options option =NewBitmapfactory.options (); Option.insamplesize =Ten;//set the different pixel. The bigger,the LowerBitmap BM = bitmapfactory.decodefile (name,option);//decode The image file according to the PathName and the pixelMyview.setimagebitmap (BM);//let the ImageView myView equal to the bitmap}}
Note: Myview.setimagebitmap (BM); is to set the id attribute in the XML in the code. (Because the picture resource is not from the app itself (or the R Resource), it comes from the SD card. )
Click here to download the source code
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Read the diagram from the SD card and display it (i)