Camera intent call

Source: Internet
Author: User

/* Activity used to identify the photo request function */
Private Static final int camera_with_data = 3023;

/* Used to identify the activity requesting gallery */
Private Static final int photo_picked_with_data = 3021;

/* Storage location of the photo */
Private Static final file photo_dir = new file (environment. getexternalstoragedirectory () + "/dcim/camera ");

Note that <uses-Permission Android: Name = "android. Permission. write_external_storage"/> permission is required for the SD card path.

If it is written hereProgramThe onactivityresult is not called after the path is taken because the program cannot access this program.

Private file mcurrentphotofile; // The image taken by the camera

Private void dopickphotoaction (){
Context context = editcontact. This;

// Wrap our context to inflate list items using correct theme
Final context dialogcontext = new contextthemewrapper (context,
Android. R. style. theme_light );
String cancel = "return ";
String [] choices;
Choices = new string [2];
Choices [0] = getstring (R. String. take_photo); // take a photo
Choices [1] = getstring (R. String. pick_photo); // select from album
Final listadapter adapter = new arrayadapter <string> (dialogcontext,
Android. R. layout. simple_list_item_1, choices );

Final alertdialog. Builder = new alertdialog. Builder (
Dialogcontext );
Builder. settitle (R. String. attachtocontact );
Builder. setsinglechoiceitems (adapter,-1,
New dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int which ){
Dialog. Dismiss ();
Switch (which ){
Case 0 :{
String status = environment. getexternalstoragestate ();
If (status. Equals (environment. media_mounted) {// determines whether an SD card exists.
Dotakephoto (); // the user clicks to get it from the camera
}
Else {
Showtoast ("No SD card ");
}
Break;

}
Case 1:
Dopickphotofromgallery (); // obtain from the album
Break;
}
}
});
Builder. setnegativebutton (cancel, new dialoginterface. onclicklistener (){

@ Override
Public void onclick (dialoginterface dialog, int which ){
Dialog. Dismiss ();
}

});
Builder. Create (). Show ();
}
}

/**
* Take a photo and get an image
*
*/
Protected void dotakephoto (){
Try {
// Launch camera to take photo for selected contact
Photo_dir.mkdirs (); // create a photo storage directory
Mcurrentphotofile = new file (photo_dir, getphotofilename (); // name the new photo file
Final intent = gettakepickintent (mcurrentphotofile );
Startactivityforresult (intent, camera_with_data );
} Catch (activitynotfoundexception e ){
Toast. maketext (this, R. String. photopickernotfoundtext,
Toast. length_long). Show ();
}
}

Public static intent gettakepickintent (file F ){
Intent intent = new intent (mediastore. action_image_capture, null );
Intent. putextra (mediastore. extra_output, Uri. fromfile (f ));
Return intent;
}

/**
* Name the obtained image at the current time.
*
*/
Private string getphotofilename (){
Date = new date (system. currenttimemillis ());
Simpledateformat dateformat = new simpledateformat (
"'Img '_ yyyy-mm-dd hh: mm: SS ");
Return dateformat. Format (date) + ". jpg ";
}

// Request the Gallery program
Protected void dopickphotofromgallery (){
Try {
// Launch picker to choose photo for selected contact
Final intent = getphotopickintent ();
Startactivityforresult (intent, photo_picked_with_data );
} Catch (activitynotfoundexception e ){
Toast. maketext (this, R. String. photopickernotfoundtext1,
Toast. length_long). Show ();
}
}

// Encapsulate the intent of the gallery request
Public static intent getphotopickintent (){
Intent intent = new intent (intent. action_get_content, null );
Intent. settype ("image /*");
Intent. putextra ("crop", "true ");
Intent. putextra ("aspectx", 1 );
Intent. putextra ("aspecty", 1 );
Intent. putextra ("outputx", 80 );
Intent. putextra ("outputy", 80 );
Intent. putextra ("Return-Data", true );
Return intent;
}

// Because camera and Gally are called, you need to determine their respective responses. This is the startactivityforresult when they are started.
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
If (resultcode! = Result_ OK)
Return;
Switch (requestcode ){
Case photo_picked_with_data: {// call the return value of Gallery
Final bitmap photo = data. getparcelableextra ("data ");
// The following figure shows the photo.
System. Out. println (photo );
// Cache the selected Image
IMG = getbitmapbyte (photo );
Meditor. setphotobitmap (photo );
System. Out. println ("set new photo ");
Break;
}
Case camera_with_data: {// The camera program returns and calls the image editing program again to trim the image.
Docropphoto (mcurrentphotofile );
Break;
}
}
}

Protected void docropphoto (file F ){
Try {
// Start gallery to edit the photo.
Final intent = getcropimageintent (URI. fromfile (f ));
Startactivityforresult (intent, photo_picked_with_data );
} Catch (exception e ){
Toast. maketext (this, R. String. photopickernotfoundtext,
Toast. length_long). Show ();
}
}

/**
* Constructs an intent for image cropping. Call the image editing program
*/
Public static intent getcropimageintent (URI photouri ){
Intent intent = new intent ("com. Android. Camera. Action. Crop ");
Intent. setdataandtype (photouri, "image /*");
Intent. putextra ("crop", "true ");
Intent. putextra ("aspectx", 1 );
Intent. putextra ("aspecty", 1 );
Intent. putextra ("outputx", 80 );
Intent. putextra ("outputy", 80 );
Intent. putextra ("Return-Data", true );
Return intent;
}

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.