Acquisition of local picture resources and video recording resources in Android

Source: Internet
Author: User
<span id="Label3"></p><p><p>Often used to the local image acquisition, as well as recording and so on, so a little tidy up a bit, the pattern is almost, we can refer to.<br>Here write the code piece package com.ly.day72_camera;</p></p><p><p>Import android.content.Intent;<br>Import android.database.Cursor;<br>Import android.graphics.Bitmap;<br>Import android.net.Uri;<br>Import android.os.Environment;<br>Import android.provider.MediaStore;<br>Import android.support.v7.app.AppCompatActivity;<br>Import android.os.Bundle;<br>Import android.util.Log;<br>Import android.view.View;<br>Import android.widget.ImageView;<br>Import android.widget.Toast;</p></p><p><p>Import java.io.File;<br>Import java.io.FileNotFoundException;<br>Import java.io.FileOutputStream;</p></p><p><p>/** <br> * Take photos <br> * Get pictures from albums <br> * Recording <br> * Video <br> */<br> public class Mainactivity extends Appcompatactivity {<br> ImageView img; <br> public static final int take_photo = 1;//result after photo <br> public static final int result_photo = 2;//cropped result <br> Public St atic final int get_photo = 3;//album gets results <br> public static final int get_voice_camera = 4;//gets The result of the recording </p></p><pre><code>@Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); img = (ImageView) Findviewbyid (r.id.img);} /** * Button Event handling * * @param view */public void Btnclick (view view) {switch (view.getid ()) {case R.id.btntakephoto ://photo Takephoto (); Break Case R.id.btngetphoto://get Getphoto () from the album; Break Case R.id.btngetvoice://get the recording Getvoice (); Break Case R.id.btngetvideo://get Camera Getvideo (); Break }}/** * Get video */private void Getvideo () {Intent Intent = new Intent (mediastore.action_video_capture); Intent.putextra (mediastore.extra_video_quality, 0);//mass 0 lossless Startactivityforresult (intent, get_voice_camera);} /** * Get system recording */private void Getvoice () {Intent Intent = new Intent (intent.action_get_content); Intent.settype ("audio/amr"); Intent.setclassname ("com.android.soundrecorder", "Com.android.soundrecorder.SoundRecorder "); Startactivityforresult (intent, get_voice_camera);} /** * get from album picture */private void Getphoto () {Intent Intent = new Intent (intent.action_pick, null); Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");//query Type Startactivityforresult ( intent, get_photo);} /** * Photo */private void Takephoto () {//open camera ACTION Intent Intent = new Intent (mediastore.action_image_capture); This parameter is the path that is stored after the photo is taken Intent.putextra (mediastore.extra_output, uri.fromfile (new File) ( Environment.getexternalstoragedirectory (), "tmp.jpg")); Startactivityforresult (intent, take_photo);} @Overrideprotected void onactivityresult (int requestcode, int resultcode, Intent data) {super.onactivityresult (request Code, resultcode, data); Switch (requestcode) {case take_photo://takes A snapshot of the returned callback function file File = new file (environment.getexternalstoragedi Rectory (), "tmp.jpg"); LOG.I ("info", "----------" + file.getpath ()); Startzoom (uri.fromfile (file)); Break Case result_photo://cropped Image return value Bundle bundle = Data.getextras (); Bitmap Bitmap = bundle.getparcelable ("data"); Get the picture returned by try {//save to local fileoutputstream fos = new FileOutputStream (environment.g Etexternalstoragedirectory () + "/tmp1.jpg"); Bitmap.compress (Bitmap.CompressFormat.JPEG, n, fos); Img.setimagebitmap (bitmap); } catch (filenotfoundexception E) {e.printstacktrace (); } break; Case Get_photo://album inside get picture startzoom (data.getdata ()); Break Case Get_voice_camera://gets The recording, or the camera (because the same processing METHOD) cursor cursor = getcontentresolver (). query (data.getdata (), null, nu ll, null, null); While (cursor.movetonext ()) {//gets the recording or the Video's storage path String path = cursor.getstring (cursor.getcolum NIndex ("_data")); Toast.maketext (mainactivity.this, path, toast.length_short). Show (); } break; }}/** * Crop Picture * * @param uri */private void startzoom (uri uri) {Intent Intent = new Intent ("com.android.camera.action.c ROP ");//open The page you want to crop intent.setdataandtype (uri," image/* ");//cropped picture, and picture type Intent.putextra (" aspectx ", 1); Represents the width-height ratio of the cropped picture Intent.putextra ("aspecty", 1); Represents the width-height ratio Intent.putextra ("outputx", 300) of the cropped picture, or the width-height Intent.putextra ("outputy", 300) of the cropped thumbnail, or the width-height of the cropped Thumbnail. T.putextra ("crop", true);//indicates Whether you need to crop the Intent.putextra ("return-data", True);//means to return the cropped picture Startactivityforresult ( intent, result_photo);} /** * Get Picture Path * * @param originaluri * @return */private String getpath (Uri originaluri) {string[] proj = {mediastore.i Mages. media.data}; Encapsulation interface for Android multimedia database cursor cursor = getcontentresolver (). query (originaluri, proj, null, null, null); According to my own understanding, this is the index value of the image that gets the user's choice int column_index = Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA); Move the cursor to the beginning, whichA very important, careless is easily caused by the Cross-border Cursor.movetofirst (); Finally get the picture path according to the index value String path = cursor.getstring (column_index); Return path;}</code></pre><p><p>}</p></p> <p><p>Acquisition of local picture resources and video recording resources in Android</p></p></span>

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.