Android2.3 select an album image or call the system to take a photo

Source: Internet
Author: User

2.3 of the tragedy bothered me. after calling the System camera function with my previous 2.2 code, I was unable to call back the onactivityresult function because I was unable to call the forward path, experience it myself, and record it.

/**
* Select the uploaded Image
*/
Private void checkimage (){
Final charsequence [] items = {"album", "photo "};
Builder = new alertdialog. Builder (productcommentactivity. This );
Builder. settitle (R. String. preferential_comment_checkimage_title). setitems (items, onclicklistener );
Builder. Create (). Show ();
}

/**
* Select the uploaded Image
*/
Private dialoginterface. onclicklistener = new dialoginterface. onclicklistener (){

@ Override
Public void onclick (dialoginterface dialog, int which ){
If (which = 1) {// take a photo
Intent getimagebycamera = new intent (mediastore. action_image_capture );
Startactivityforresult (getimagebycamera, cameraman_image );
} Else {
Intent getimage = new intent (intent. action_get_content );
Getimage. addcategory (intent. category_openable );
Getimage. settype ("image /*");
Startactivityforresult (intent. createchooser (getimage, "select picture"), select_image );
}
}
};

/**
* Select the image callback function.
*/
@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
Bitmap bitmap = NULL;
If (result_ OK = resultcode ){
If (requestcode = select_image) {// select an album
Bitmap = checkimage (data );
} Else if (requestcode = cameraman_image) {// photograph
Super. onactivityresult (requestcode, resultcode, data );
Date = new date (system. currenttimemillis ());
Simpledateformat dateformat = new simpledateformat ("'img '_ yyyy_mm_dd_hhmmss ");
String imagename = dateformat. Format (date) + ". jpg ";

Bundle extras = data. getextras ();
Bitmap = (Bitmap) extras. Get ("data ");
String fileparth = savecommentimage (imagename, bitmap );
If (fileparth! = NULL &&! Fileparth. Equals ("")){
Uploadimage_parth = fileparth; // image name (required for personal projects)
}
}
}
If (Bitmap! = NULL ){
Bitmap = convertbitmappix (bitmap, commentimagewidth, commentimageheight );
Product_commentratingusericonimageview.setimagebitmap (Bitmap );
}
}

/**
* Obtain the image selected from the album or SD card.
*
* @ Param data
* @ Return
*/
@ Suppresswarnings ("finally ")
Private bitmap checkimage (intent data ){
Bitmap bitmap = NULL;
Cursor cursor = NULL;
Try {
Uri originaluri = data. getdata ();
String Path = originaluri. tostring ();
String [] proj = {mediastore. Images. Media. Data };
Cursor = managedquery (originaluri, proj, null );
If (cursor! = NULL ){
Int column_index = cursor. getcolumnindexorthrow (mediastore. Images. Media. data );
Cursor. movetofirst ();
// Obtain the image path based on the index value
Path = cursor. getstring (column_index );
Cursor. Close ();
}
Path = path. substring (path. indexof ("/sdcard"), path. Length ());
Bitmap = filehelper. getcommentimage (PATH );
If (Bitmap! = NULL ){
Uploadimage_parth = path;
}
} Catch (exception e ){
Log. E ("checkimage", E. getmessage ());
} Finally {
If (cursor! = NULL) cursor. Close ();
Return bitmap;
}
}

/**
* Save the photo image to the default album.
*
* @ Param name: image name
* @ Param bitmap: Image Object
* @ Return
*/
Public static string savecommentimage (string name, Bitmap bitmap ){
String Parth = "";
Try {
String status = environment. getexternalstoragestate ();
If (status. Equals (environment. media_mounted )){
// Save the image to the default album.
String fileparth = environment. getexternalstoragedirectory (). getabsolutepath () + constant. image_folder_name + name;
File myfile = new file (fileparth );
Myfile. createnewfile ();
Fileoutputstream fout = NULL;
Fout = new fileoutputstream (myfile );
Bitmap. Compress (bitmap. compressformat. JPEG, 100, fout );
Fout. Flush ();
Fout. Close ();
Parth = fileparth;
}
} Catch (exception e ){
Log. E ("Save bitmap failed", E. getmessage ());
}
Return Parth;
}

/**
* Convert the image size
*
* @ Param bitmap: original image object
* @ Param newwidth: width of the new image
* @ Param newheight: height of the new image
* @ Return
*/
@ Suppresswarnings ("finally ")
Public static bitmap convertbitmappix (Bitmap bitmap, int newwidth, int newheight ){
Bitmap newbitmap = NULL;
Try {
If (Bitmap! = NULL ){
Int width = bitmap. getwidth ();
Int Height = bitmap. getheight ();
// Calculate the scaling rate
Float scalewidth = (float) newwidth)/width;
Float scaleheight = (float) newheight)/height;
// Create a matrix object for image operations
Matrix matrix = new matrix ();
Matrix. postscale (scalewidth, scaleheight );
// Create a new image
Newbitmap = bitmap. createbitmap (bitmap, 0, 0, width, height, matrix, true );
}
} Catch (exception e ){
Log. E ("convertbitmappix", E. getmessage ());
} Finally {
Return newbitmap;
}
}

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.