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!