Android multimedia learning 1: a simple example of image in Android.

Source: Internet
Author: User

In multimedia applications, image is the most basic function module. Next we will look at how to obtain and store images in Android. The image acquisition and storage function embedded in Android gives us a comprehensive understanding of the entire media framework and lays the foundation for learning audio and video.
1. You can call the camera app that comes with Android to obtain the image. This application contains an intent-filter. Use
Intent intent = new intent (mediastore. action_image_capture); startactivityforresult (intent) can start the camera application.
2. image storage. The Android system contains a multi-media library, which includes all the image, video, and audio data. The mediastore object can be used to access relevant data.

Let's talk less. Let's look at the example. This is an English document. I think it is well written. I have translated it myself and added a lot of comments. For query.

 

Package demo. camera; <br/> Import Java. io. file; <br/> Import android. app. activity; <br/> Import android. content. contentvalues; <br/> Import android. content. intent; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. OS. environment; <br/> Import android. provider. mediastore; <br/> Import android. UTI L. log; <br/> Import android. view. display; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. imageview; <br/>/** <br/> * the first example of multimedia, this section describes how to obtain and store images. <br/> * image retrieval can be obtained through the camera app that comes with Android. <br/> * mediastore objects are required for image storage. Multi-media library in Android. <Br/> * @ author administrator <br/> */<br/> public class mainactivity extends activity {</P> <p> Private Static final int result_code = 1; <br/> private button btncamera; <br/> private imageview; </P> <p> private URI imagefilepath; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main ); </P> <p> imageview = (imageview) This. findviewbyid (R. id. imageview); <br/> btncamera = (button) This. findviewbyid (R. id. camera); <br/> btncamera. setonclicklistener (new view. onclicklistener () {</P> <p> @ override <br/> Public void onclick (view V) {</P> <p>/** <br/> * Because Camara returns a thumbnail, we can pass it to the extra_output parameter, <br/> * stores the images obtained using camera in a specified Uri. <Br/> * the image is stored on sdcard and the file name is 123.jpg. <br/> * imagefilepath = environment. getexternalstoragedirectory (). getabsolutepath () + "123.jpg"; <br/> * file = new file (imagefilepath); // create a file <br/> * URI imageuri = Uri. fromfile (File); <br/> * However, Android provides a multi-media library, which stores all the multimedia data on the device. Therefore, <br/> * You can store the obtained image in the multi-media library. <Br/> * Android provides the mediastore class, which is a contentprovider that manages the built-in and external multimedia files on the device, <br/> * contains the data information of each multimedia file. <Br/> * to store data in a multi-media library, use the contentresolver object to manipulate the mediastore object <br/> * In mediastore. images. media has two URI constants: external_content_uri and internal_content_uri <br/> * The first URI corresponds to the external device (sdcard). The second URI corresponds to the internal storage location of the system device. <Br/> * multimedia files are generally large, we select the external storage method <br/> * by using the insert method of the contentresolver object, We can insert a piece of data into mediastore <br/> * so that when we retrieve the image, the file path is no longer used, but the URI returned when the insert data is used, obtain an inputstream <br/> * and send it to bitmapfactory <br/> */<br/> // start camera here. <Br/> // A intent-filter is defined in camera, where action is Android. media. action. image_capture <br/> // we recommend that you do not use this parameter directly, but use the constant action_image_capture in mediastore. <br/> // The constant corresponds to the above action <br/> intent = new intent (mediastore. action_image_capture); </P> <p> // here we insert a piece of data, contentvalues is the data information we want this record to contain when it is created <br/> // The data names have been used as constants in mediastore. images. media, some are stored in mediastore. mediacolumn is in <br/> // contentvalues values = new con Tentvalues (); <br/> contentvalues values = new contentvalues (3); <br/> values. put (mediastore. images. media. display_name, "testing"); <br/> values. put (mediastore. images. media. description, "this is description"); <br/> values. put (mediastore. images. media. mime_type, "image/JPEG"); <br/> imagefilepath = mainactivity. this. getcontentresolver (). insert (mediastore. images. media. external_content_uri, values); <br/> Intent. putextra (mediastore. extra_output, imagefilepath); // specify the file storage method and URI to the camera application. </P> <p> // After the camera application is called, you can return the image obtained by camera. <br/> // Therefore, startactivityforresult is used to Start camera. <br/> startactivityforresult (intent, result_code ); </P> <p >}< br/>}); <br/>}< br/>/** <br/> * to obtain the image information returned by camera, override this method. <Br/> */<br/> @ override <br/> Public void onactivityresult (INT requestcode, int resultcode, intent data) {<br/> super. onactivityresult (requestcode, resultcode, data); <br/> If (resultcode = result_code) {<br/> // The data returned by camera <br/> // the image data returned by the camera application is a camera object, stored in an extra domain named Data <br/> // store the obtained image in imageview </P> <p> try {<br/> bundle extra = data. getextras (); <br/>/** <br/> * However, to save memory consumption The returned image is a 121*162 thumbnail. <Br/> * how do we return the big image we need? See <br/> * But store the image. With the storage location of images, can images be directly displayed? <br/> * this problem is designed to process and display images, which consumes a lot of memory, for a PC, it may be nothing, but for a mobile phone, <br/> * may cause your application to die due to memory depletion. But fortunately, Android considers this for us. <br/> * The bitmapfactory class and its internal class bitmapfactory can be used in Android. options to implement image processing and display <br/> * bitmapfactory is a tool class that contains many methods to obtain bitmap. The bitmapfactory. Options class has an insamplesize. For example, if the value is set to 8, the size of the image loaded into the memory will be <br/> * the size of the original image is 1/8. This greatly reduces the memory consumption. <Br/> * bitmapfactory. options op = new bitmapfactory. options (); <br/> * op. insamplesize = 8; <br/> * bitmap PIC = bitmapfactory. decodefile (imagefilepath, OP); <br/> * this is a quick way to load a large image, because he does not need to consider the size of the entire display screen and the original size of the image <br/> * However, sometimes I need to scale the screen accordingly. How can this problem be solved? <Br/> */<br/> // obtain the Screen Object first. <br/> display = This. getwindowmanager (). getdefadisplay display (); <br/> // obtain the screen width and height <br/> int DW = display. getwidth (); <br/> int DH = display. getheight (); <br/>/** <br/> * to calculate the scaling ratio, we need to obtain the size of the entire image, instead of the image. <br/> * bitmapfactory. the options class has a Boolean variable injustdecodebounds and sets it to true <br/> *. In this way, we get the image size instead of loading the image. <Br/> * When we set this value, we can start from bitmapfactory. <br/> */<br/> bitmapfactory. options op = new bitmapfactory. options (); <br/> // op. insamplesize = 8; <br/> op. injustdecodebounds = true; <br/> // bitmap PIC = bitmapfactory. decodefile (imagefilepath, OP); // after this method is called, The outwidth and outheight values in OP will be available <br/> // because mediastore storage is used, here, the format of the input stream is obtained based on the URI <br/> bitmap PIC = bitmapfactory. decodestream (this <Br/>. getcontentresolver (). openinputstream (imagefilepath), <br/> null, OP); <br/> int wratio = (INT) math. ceil (op. outwidth/(float) dw); // calculate the width ratio <br/> int hratio = (INT) math. ceil (op. outheight/(float) DH); // calculate the height ratio <br/> log. V ("width ratio:", wratio + ""); <br/> log. V ("height ratio:", hratio + ""); <br/>/** <br/> * next, we need to determine whether to scale and whether to scale the width or height. <Br/> * If the height and width are not all beyond the screen, no scaling is required. <Br/> * If the height and width exceed the screen size, how do I select scaling? <br/> * This requires determining the size of wratio and hratio. <br/> * the larger one will be scaled because, small ones should be scaled at the same rate automatically. <Br/> * Whether the insamplesize variable is used for scaling <br/> */<br/> If (wratio> 1 & hratio> 1) {<br/> If (wratio> hratio) {<br/> op. insamplesize = wratio; <br/>} else {<br/> op. insamplesize = hratio; <br/>}< br/> op. injustdecodebounds = false; // be sure to set it to false here, because we set it to true to get the image size. <br/> PIC = bitmapfactory. decodestream (this. getcontentresolver () <br/>. openinputstream (imagefilepath), null, OP); <br/> imageview. setimagebitmap (PIC); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>}

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.