Android Development Practice: Write your own picture tailoring application (2)

Source: Internet
Author: User

The previous article mainly introduces the basic features and usage of my open source image clipping library (imagecropper) on GitHub, starting with this article and slowly introducing some of the knowledge points and techniques involved in developing image tailoring applications.


In fact, the Android system itself also provides a picture clipping module, we can directly through the intent to call the system's picture clipping function, this article we first understand how the system comes with the picture clipping function is how to invoke it.


    1. Get the URL address of the cropped picture


Since the picture is cropped, it is necessary to have a cropped picture, because the picture data is generally large, in order to prevent memory overflow, the normal app and the Android system picture clipping app is the URL to pass the image address. This URL is not the same as the Web URL we often see, it is not the beginning of HTTP, but a string that begins with file or content, for example:


"File:///sdcard/test.jpg" "content://media/external/images/media/21936"

Here, we first describe how to get the image URL:


(1) Get the URL of the picture from the SDcard


Assuming you know that the path to the picture is in "/sdcard/test.jpg", you can get the URL in this way:


Uri Imageuri = uri.fromfile (New File ("/sdcard/test.jpg"));

Of course, if the picture is obtained from the network and does not exist in SDcard, you can save a temporary file to SDcard, and then get the URL by the above method.


Note: URLs obtained in this way usually begin with "file://".


(2) Get the URL of the picture from the multimedia database


The Android system periodically scans multimedia files stored in the system in the background, such as music, pictures, and videos, and the information is stored in the system's multimedia database, located in/data/data/com.android.providers.media/ Databases, we can retrieve the URL of the image by retrieving the database (for example, by retrieving the database to get the last added/modified photo URL), or by invoking the system's image selector via Intent.action_pick to select a picture. The picture selector returns the URL address of the picture into the intent data.


The latter is more application, we mainly introduce the latter, that is, through the Intent.action_pick to get the image URL address, the method is as follows:


public void Pickimage () {Intent Intent = new Intent (Intent.action_pick);    Intent.settype ("image/*"); Startactivityforresult (intent,request_code_pick_image);}         @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode! = RESULT_OK) {    Return        } if (Requestcode = = request_code_pick_image) {Uri Imageuri = Data.getdata (); //......    }}


Note: URLs obtained in this way usually begin with "Content://media".


(3) Call the system's camera and take a picture.


Of course, the cropped picture can also be a photo taken by camera, as follows:


uri pictureurl = uri.fromfile (New File ("/sdcard/ Temp.jpg "));p ublic void takenpicture ()  {    Intent intent =  New intent (mediastore.action_image_capture);     intent.putextra (MediaStore.EXTRA_ output,pictureurl );     startactivityforresult (intent,request_code_taken_picture);} @Overrideprotected  void onactivityresult (int requestcode, int resultcode, intent  data)  {    if  (RESULTCODE != RESULT_OK)  {         return;    }    if ( requestCode  == REQUEST_CODE_TAKEN_PICTURE )  {        uri  imageUri = pictureURL;        //......     }} 


Through the code you may have noticed, in fact, this way to get the image URL, and the first way is the same, through the image of the storage path to the conversion, but passed to the system camera application.


2. The picture clipping function of the system is called via intent


With the URL of the picture, it is very simple to call the picture of the system, just build a intent object and set the relevant parameters, and use the example below:


Public void test ( Uri imageUri )  {    uri croppeduri  = uri.fromfile (New file ("/sdcard/cropped.jpg"));     startsystemcropimage ( Imageuri,croppeduri);} Public void startsystemcropimage ( Uri src, Uri dst )  {     intent intent = new intent ("Com.android.camera.action.CROP");         intent.putextra ("Crop",  "true");         //  set the source/destination address of the cropped image Url    intent.setdataandtype (src, "image/*");     intent.putextra (MEDIASTORE.EXTRA_OUTPUT,DST);    //  set the aspect ratio of the cropped picture      //intent.putextra ("Aspectx",  2);        // Intent.putextra ("Aspecty",  1);        //  the width and height values of the fixed clip image      //inTent.putextra ("Outputx",  680);      //intent.putextra ("OutputY",  480);             //  to prevent memory limitations and the data returned by each vendor are not uniform, It is not recommended to use this returned data directly, but instead to return the data Url    intent.putextra ("Return-data",  false);         startactivityforresult (intent, request_code_system_cropper);} @Overrideprotected  void onactivityresult (int requestcode, int resultcode, intent  data)  {    if  (RESULTCODE != RESULT_OK)  {         return;    }    if ( requestCode  == REQUEST_CODE_IMAGE_CROPPER )  {                 uri croppeduri = data.getextras (). GetParcelable ( Mediastore.extra_output);         inputstream in = null;         try {            in =  Getcontentresolver (). Openinputstream (Croppeduri);             bitmap b = bitmapfactory.decodestream (in);             mimageview.setimagebitmap (b);                     }          catch  (filenotfoundexception e)  {             e.printstacktrace ();         }             }     Super.onactivityresult (Requestcode, resultcode, data);} 


3. Summary


Note the ability to add read-write SDcard:


<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

About the call system picture clipping application is introduced here, have any question welcome the message or the letter [email protected] exchange.


This article is from the "Shadow Three People" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1602611

Android Development Practice: Write your own picture tailoring application (2)

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.