Android starts to take a photo/video in the program or select an image/video, and create a thumbnail to save it.

Source: Internet
Author: User

Sometimes we need to select an image as the Avatar or upload it to the album. In this case, we can choose to start the camera or choose from the image library.

First, let's see how to start the camera and take the photo.

The following code starts the camera and returns the photo:

Public void capturepicture () {// start and save the photo to the temporary file intent = new intent (); intent. setaction (mediastore. action_image_capture); intent. putextra (mediastore. extra_output, Uri. fromfile (File); startactivityforresult (intent, request_capture_image );}

The following code starts the camera and returns the recorded video:

Public void capturevideo () {intent intent2 = new intent (); intent2.setaction (mediastore. action_video_capture); intent2.putextra (mediastore. extra_video_quality, 0); // set it to startactivityforresult (intent2, request_capture_video) of low quality );}

The following code is used to open the Image Library (which is the default image selector) and select an image or video:

public void choosePicture(){    Intent intent3 = new Intent();    intent3.setType("image/*");    intent3.setAction(Intent.ACTION_GET_CONTENT);    startActivityForResult(intent3, REQUEST_CHOOSE);}

In this way, the whole process is completed. After taking the photo, we press "OK", or click "select a photo" and return to the interface of our program. How can we get the photo just now? Note that the startactivityforresult method is used in the above Code. Therefore, we need to overload the onactivityresult method to obtain the returned data. The Code is as follows:

@ Override protected void onactivityresult (INT requestcode, int resultcode, intent data) {super. onactivityresult (requestcode, resultcode, data); Switch (resultcode) {Case result_ OK: URI uri = data. getdata (); // get URI string fpath = uri2filepath (URI); // convert it to a path/* Do the processing you want **/break ;}}

This completes the entire process.

The following is a method for creating a thumbnail and converting a URI into a file path!
Make a thumbnail of an image or video:

/** Create a thumbnail and return the thumbnail file path */Public String createthumbnail (bitmap source, string filename) {int oldw = source. getwidth (); int oldh = source. getheight (); int W = math. round (float) oldw/max_sizes); // max_size is the maximum size of the thumbnail int H = math. round (float) oldh/max_sizes); int NEWW = 0; int newh = 0; If (W <= 1 & H <= 1) {return savebitmap (source, filename);} int I = W> H? W: H; // obtain the scaling ratio NEWW = oldw/I; newh = oldh/I; bitmap imgthumb = thumbnailutils. extractthumbnail (source, NEWW, newh); // key code !! Return savebitmap (imgthumb, filename); // Note: The savebitmap method is the private method used to save the image and return the path.}/** create a video thumbnail, returns the thumbnail file path */Public String createvideothumbnail (string filepath, string filename) {bitmap videothumb = thumbnailutils. createvideothumbnail (filepath, thumbnails. mini_kind); // key code !! Return savebitmap (videothumb, filename); // Note: The savebitmap method is the private Method for saving the image and returning the path}

Convert URI to file path:

/** Convert URI to file path */private string uri2filepath (URI) {string [] proj = {mediastore. images. media. data}; cursor = managedquery (Uri, proj, null); int Index = cursor. getcolumnindexorthrow (mediastore. images. media. data); cursor. movetofirst (); string Path = cursor. getstring (INDEX); return path ;}

OK! Close the work!

Related Article

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.