Examples of Android photo taking, video recording, and Recording Code

Source: Internet
Author: User

Package com. Cons. DCG. Collect;

Import java. Io. file;

Import java. Text. simpledateformat;

Import java. util. arraylist;

Import java. util. date;

Import java. util. hashmap;

Import java. util. List;

Import Android. App. activity;

Import Android. App. notification;

Import Android. App. icationicationmanager;

Import Android. App. pendingintent;

Import Android. App. progressdialog;

Import Android. content. intent;

Import Android. database. cursor;

Import android.net. Uri;

Import Android. OS. asynctask;

Import Android. OS. Bundle;

Import Android. OS. environment;

Import Android. provider. mediastore;

Import Android. View. keyevent;

Import Android. View. Menu;

Import Android. View. menuinflater;

Import Android. View. menuitem;

Import Android. View. view;

Import Android. View. View. onclicklistener;

Import Android. widget. adapterview;

Import Android. widget. arrayadapter;

Import Android. widget. Button;

Import Android. widget. edittext;

Import Android. widget. spinner;

Import Android. widget. Toast;

Public class problemreport extends activity implements onclicklistener {

Private Static final int result_capture_image = 1; // the photo's requestcode
Private Static final int request_code_take_video = 2; // The requestcode of the camera.
Private Static final int result_capture_recorder_sound = 3; // requestcode of the recording
private string strimgpath = ""; // absolute path of the photo file
private string strvideopath = ""; // absolute path of the video file
private string strrecorderpath = ""; // absolute path of the recording file

@ Override

Protected void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

This. setcontentview (R. layout. problem_report );
Arrears
}

@ Override

Protected void onactivityresult (INT requestcode, int resultcode, intent data ){

Super. onactivityresult (requestcode, resultcode, data );

Switch (requestcode ){

Case result_capture_image: // take a photo

If (resultcode = result_ OK ){

Toast. maketext (this, strimgpath, Toast. length_short). Show ();

}

Break;

Case request_code_take_video: // video shooting

If (resultcode = result_ OK ){

Uri urivideo = data. getdata ();

Cursor cursor = This. getcontentresolver (). Query (urivideo, null );

If (cursor. movetonext ()){

/* _ DATA: absolute path of the file, _ display_name: file name */

Strvideopath = cursor. getstring (cursor. getcolumnindex ("_ DATA "));

Toast. maketext (this, strvideopath, Toast. length_short). Show ();

}

}

Break;

Case result_capture_recorder_sound: // recording

If (resultcode = result_ OK ){

Uri urirecorder = data. getdata ();

Cursor cursor = This. getcontentresolver (). Query (urirecorder, null );

If (cursor. movetonext ()){

/* _ DATA: absolute path of the file, _ display_name: file name */

Strrecorderpath = cursor. getstring (cursor. getcolumnindex ("_ DATA "));

Toast. maketext (this, strrecorderpath, Toast. length_short). Show ();

}

}
Software Development
Break;

}

}



/**

* Photography

*/

Private void cameramethod (){

Intent imagecaptureintent = new intent (mediastore. action_image_capture );

Strimgpath = environment. getexternalstoragedirectory (). tostring () + "/consdcgmpic/"; // folder for storing photos

String filename = new simpledateformat ("yyyymmddhhmmss"). Format (new date () + ". jpg"; // name the photo

File out = new file (strimgpath );

If (! Out. exists ()){

Out. mkdirs ();

}

Out = new file (strimgpath, filename );

Strimgpath = strimgpath + filename; // the absolute path of the photo

Uri uri = 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 = 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 = new intent (intent. action_get_content);
intent. settype ("audio/AMR");
startactivityforresult (intent, result_capture_recorder_sound);

}

/**
* message
* @ Param text
* @ Param duration

*/
private void showtoast (string text, int duration) {
toast. maketext (problemreport. this, text, duration ). show ();

}

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.