Android photos get pictures and get pictures after clipping settings to ImageView

Source: Internet
Author: User

OK, this time the project needs to set up the Avatar function, so make a summary, directly into the topic bar.

Let's talk about how

    • Take a photo with your Android built-in camera and get this picture.

Directly on the code:

Intent intentfromcapture = new Intent (mediastore.action_image_capture); Uri Imageuri = uri.fromfile (New File (environment.getexternalstoragedirectory () + "/zxy/image/temp.png"));// Specify the photo save path (SD card), Temp.jpg is a temporary file, each time the picture will be replaced Intentfromcapture.putextra (Mediastore.extra_output,imageuri); Startactivityforresult (Intentfromcapture,camera_request_code);
Mediastore.action_image_capture is called the system built-in camera, for the corresponding function of the ACTION string see my other blog post, " 

Android Development intent.action The common role of various action, and then use mediastore.extra_output action to tell the system to put in a media file, so where does this media file go? is to tell it by URI, we put the picture after the photo in the phone's internal SD card root directory in the Zxy directory under the image directory, and specify that the photo name is Temp.png, the directory is displayed as:/storage/sdcard0/zxy/image/temp.png. The URI of a file object is then obtained by using the Uri.fromfile (file file) method, so that we put the photo we just tookthe/storage/sdcard0/zxy/image directory.

So, how do we get this picture?

1, if we take photos, to directly use this picture as what background, Avatar Ah and so on, generally this use of the most, we can pass the request code we just passed to judge and set this picture, this need to rewrite the activity of the Onactivityresult () method, Directly on the code:

@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult ( Requestcode, ResultCode, data), if (resultcode! = activity.result_canceled) {//Through the result code to determine whether to take the picture switch (requestcode) {// The request code to determine which of the requested data case Image_request_code://.............break;case camera_request_code:// It is important to note that the image information returned directly here is passed through intent data, Data.getdata () obtains the URI of the picture, and then the URI of the image to do the corresponding thing//.............break;}}
2, if only a simple photographic, we can use this method,take out the photos bybitmapfactory.decodefile (String pathName) gets the picture by passing in the path of a picture, or by first getting the image's input stream and then passingBitmapfactory.decodestream (InputStream is) to get the picture ..... And so on, there are a lot of methods anyway.


By the way to popularize the various methods of Android inside get directory:

1, Environment.getexternalstoragedirectory () to get the directory is:/storage/sdcard0

2, Environment.getdatadirectory () to get the directory is:/data

3, Environment.getrootdirectory () to get the directory is:/system

Our app is installed in the/data/data directory, and the general Data directory we generally create in the built-in SD card /storage/sdcard0 directory, usually the name of the app to create a folder, and then create another directory, Generally private data we can create in the app directory, that is, the/data/data/app package name/created private data directory.

    • Select a picture in the album to use the selected picture to set the avatar or background, directly on the code:

Intent intentfromgallery = new Intent (intent.action_get_content); Intentfromgallery.settype ("image/*"); Startactivityforresult (Intentfromgallery,image_request_code);
Intent.action_get_content is to show that this aciton is used to get a content, then what is this content? is to filter by Settype (). And thenrewriting the activity'sThe Onactivityresult () method gets the URI of the selected picture by Data.getdata (), and the selected picture can be obtained in both ways:

1. Through

To get the bitmap object.

2, through the URI to get the corresponding path, through the path to get the file object or IO stream to perform each operation.

Get the picture path private String getimgpath (Intent data) {if (data! = null) {URI uri = Data.getdata (); String[] Pojo = {MediaStore.Images.Media.DATA}; cursor cursor = this.getcontentresolver (). Query (URI, Pojo, null,null, NULL); String Path = null;if (cursor! = NULL) {int columnindex = Cursor.getcolumnindex (pojo[0]); Cursor.movetofirst ();//move the cursor to the beginning One position path = cursor.getstring (columnindex); Cursor.close ();} return path;} else {toast.maketext (userlogin.this, "You did not select Picture", Toast.length_short). Show (); return null;}}

    • How to cut the picture after the photo or the pictures selected in the album and then use the picture after the cut? By passing a URI or path, you can cut the image.

Crop picture public void Startphotozoom (Uri uri) {Intent Intent = new Intent ("Com.android.camera.action.CROP");// Initiates the cut action Intent.setdataandtype (URI, "image/*");//sets the URI and type of the cut picture Intent.putextra ("crop", "true");// The signal Intent.putextra of the shear action ("Aspectx", 1),//x and y are equal than the zoom Intent.putextra ("Aspecty", 1); Intent.putextra ("Outputx", 320); Intent.putextra ("Outputy", 320);//the size of the picture after shearing Intent.putextra ("Return-data", true);// Whether to return the cut image via data to Intent.putextra ("OutputFormat", Bitmap.CompressFormat.PNG.toString ());//The output format  of the picture Intent.putextra ("Nofacedetection", true);  Turn off Face recognition//Set cut picture save location Uri Cropuri = Uri.fromfile (New File (Environment.getexternalstoragedirectory (). GetPath () + "/zxy /image/crop.png ")); Intent.putextra (Mediastore.extra_output,cropuri); Startactivityforresult (Intent, RESULT_ Request_code);}

There are a lot of parameters in the cutting action, here is a look:

Exta Options Table for image/* crop:

Additional options Data type Describe
Crop String Send clipping signal
Aspectx Int Scale in the x direction
Aspecty Int Proportions in the Y direction
Outputx Int Width of the cropping area
Outputy Int The height of the cropping area
Scale Boolean Whether to preserve proportions
Return-data Boolean Whether to keep data back in bitmap
Data Parcelable The corresponding bitmap data
Circlecrop String Circular cropping area?
Mediastore.extra_output ("OUTPUT") Uri Point the URI to the corresponding file:///..., see the code example
OutputFormat String Output format, generally set to BITMAP format: Bitmap.CompressFormat.JPEG.toString ()
Nofacedetection Boolean Whether to remove the face recognition feature
These parameters can be randomly matched, here we cut the picture from the album turned, of course, you can also do a separate cut function, only need to wear a URI can be.

Here the picture is cut, by the request code to judge in the Onactivityresult (), and then passed over a intent type of data, we can just set the position of the cut image to get the cut picture, can also be obtained by URI, or converted to a path to get , or directly through

Bundle extras = Data.getextras (); if (extras! = null) {Bitmap photo = extras.getparcelable ("Data");D rawable drawable = new Bitmapdrawable (null, photo);}
To get this picture, here we use this method to get, more simple.


"Note" When we use the cut image, we usually set the property to only set some smaller values, like Intent.putextra ("Outputx", "320"); Intent.putextra ("outputy"); After the cut picture size can not be set too large, after testing to 640 will appear stuck phenomenon, why? The reason is that the maximum value of the intent data field is approximately 1M, so the bitmap of the picture will fail when it exceeds 1M. Usually we just set the avatar can use this method, then if we want to cut to show some of the larger pictures? How do you do it?

The method is set to Return-data to false, not from the data domain to obtain the picture, but rather over the bridge, through the path we have just set after the image of the directory and the name to obtain the picture, it can be perfectly displayed. If you do not set Return-data to false, it will be returned by default via data, resulting in a stuck image.

Return-data: is to save the result in data returned, in Onactivityresult, directly call Intent.getdata () can get the value, this is set to Fase, is not to save it in data

Mediastore.extra_output: since we don't allow it to be stored in the data domain of intent, we always have to have a place to save our images, this parameter is to transfer the saved address, the corresponding value of the URI is the specified save address.







Android photos get pictures and get pictures after clipping settings to ImageView

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.