Android recording, photo taking, video recording

Source: Internet
Author: User

Android recording, photo taking, video recording

Import java. io. file; import java. text. simpleDateFormat; import java. util. date; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. app. activity; import android. content. intent; import android. database. cursor; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; imp Ort android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private static final int RESULT_CAPTURE_IMAGE = 1; // the photo's requestCodeprivate static final int REQUEST_CODE_TAKE_VIDEO = 2; // requestCodeprivate static final int RESULT_CAPTURE_RECORDER_SOUND = 3; // The requestCodeprivate String strImgPath = ""; // the absolute path of the photo file is private String strVideoPath = ""; // absolute path of the video file p Rivate String strRecorderPath = ""; // the absolute path of the recording file: Button buttonShot; Button buttonVideo; Button buttonRecorder; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); this. setContentView (R. layout. activity_main); buttonShot = (Button) findViewById (R. id. buttonShot); buttonShot. setOnClickListener (this); buttonVideo = (Button) findViewById (R. id. buttonVideo); buttonVideo. SetOnClickListener (this); buttonRecorder = (Button) findViewById (R. id. buttonRecorder); buttonRecorder. setOnClickListener (this) ;}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); switch (requestCode) {case RESULT_CAPTURE_IMAGE: // if (resultCode = RESULT_ OK) {Toast. makeText (this, strImgPath, Toast. LENGTH_SHO RT ). show () ;}break; case REQUEST_CODE_TAKE_VIDEO: // The video if (resultCode = RESULT_ OK) {Uri uriVideo = data. getData (); Cursor cursor = this. getContentResolver (). query (uriVideo, null); if (cursor. moveToNext () {/* _ data: absolute file path, _ display_name: file name */strVideoPath = cursor. getString (cursor. getColumnIndex ("_ data"); Toast. makeText (this, strVideoPath, Toast. LENGTH_SHORT ). show () ;}} break; case RE SULT_CAPTURE_RECORDER_SOUND: // if (resultCode = RESULT_ OK) {Uri uriRecorder = data. getData (); Cursor cursor = this. getContentResolver (). query (uriRecorder, null); if (cursor. moveToNext () {/* _ data: absolute file path, _ display_name: file name */strRecorderPath = cursor. getString (cursor. getColumnIndex ("_ data"); Toast. makeText (this, strRecorderPath, Toast. LENGTH_SHORT ). show () ;}} break ;}/ *** camera function */p Rivate void cameraMethod () {Intent imageCaptureIntent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); strImgPath = Environment. getExternalStorageDirectory (). toString () + "/CONSDCGMPIC/"; // String fileName = new SimpleDateFormat ("yyyyMMddHHmmss "). format (new Date () + ". jpg "; // name of the photo File out = new File (strImgPath); if (! Out. exists () {out. mkdirs ();} out = new File (strImgPath, fileName); strImgPath = strImgPath + fileName; // The absolute Uri uri of the photo = Uri. fromFile (out); imageCaptureIntent. putExtra (MediaStore. EXTRA_OUTPUT, uri); imageCaptureIntent. putExtra (MediaStore. EXTRA_VIDEO_QUALITY, 1); startActivityForResult (imageCaptureIntent, RESULT_CAPTURE_IMAGE);}/*** video shooting */private void videoMethod () {Intent intent = new Intent (MediaStore. ACTION_VIDEO_CAPTURE); intent. putExtra (MediaStore. EXTRA_VIDEO_QUALITY, 0); startActivityForResult (intent, REQUEST_CODE_TAKE_VIDEO);}/*** recording function */private void soundRecorderMethod () {Intent intent = new Intent (Intent. ACTION_GET_CONTENT); intent. setType ("audio/amr"); startActivityForResult (intent, RESULT_CAPTURE_RECORDER_SOUND );} /*** prompt message ** @ param text * @ param duration */private void showToast (String text, int duration) {Toast. makeText (MainActivity. this, text, duration ). show ();} public void onClick (View v) {int id = v. getId (); switch (id) {case R. id. buttonShot: cameraMethod (); break; case R. id. buttonVideo: videoMethod (); break; case R. id. buttonRecorder: soundRecorderMethod (); break ;}}}

 

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.