Read the diagram from the SD card and display it (i)

Source: Internet
Author: User

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)

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.