Project to pick out a small function module, there is a need for friends to learn from the next.
:
Specific features: Save the captured photos to SDcard
Specific requirements:
1. Picture name Save Format: Image Name _ Phone model _ random 4-digit. jpg
2. Image decomposition, compression, display
3. Write SDcard card Save
Specific implementation:
1. Defining Layout Files
2. Writing the business code
This is only used to swap camera photos, picture display, save to introduce and provide source download
After clicking the button:
@Override public void OnClick (View v) {switch (V.getid ()) {r.id.btn_getphoto:m Photoname = Metphotoname.geteditabletext (). toString (); Mmodel = Metmodel.geteditabletext (). toString (); if (Textutils.isempty (mphotoname) | | Textutils.isempty (Mmodel)) {Toast.maketext (this, "Please enter the picture name and model", 0). Show (); Return Final String state = Environment.getexternalstoragestate (); if (Environment.MEDIA_MOUNTED.equals (state)) {final Intent takepictureimintent = new Intent (mediastore . Action_image_capture); Contentvalues values = new Contentvalues (); Mphotouri = Getcontentresolver (). Insert (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); Takepictureimintent.putextra (Android.provider.MediaStore.EXTRA_OUTPUT, Mphotouri); Startactivityforresult (TakepictureiminteNt,request_code_take_picture); } else {Toast.maketext (this, "Please check memory card", 0). Show (); } break; Default:break; } }
Onactivityresult Callback:
/* (non-javadoc) * @see android.app.activity#onactivityresult (int, int, android.content.Intent) */@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {switch (Requestcode) { Case Request_code_take_picture://Get picture by photo if (ResultCode = = ACTIVITY.RESULT_OK) { Final URI uri = Mphotouri; LOG.E ("Mainactivity", uri+ ""); if (uri! = null) {processpicture (URI); }//Prompt to save picture alertdialog.builder Builder = new Alertdialog.builder (this); Builder.settitle ("hint"); Builder.setmessage ("Do you want to save the picture?") "); Builder.setnegativebutton ("Cancel", null); Builder.setpositivebutton ("Yes", new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterfacedialog, int which) {String Resetphotoname = mphotoname+ "_" +mmodel+ "_" +utils.getrandomstring (4) ; Utils.savephototosdcard (Bitmap,utils.genprojectpath (), resetphotoname); Toast.maketext (mainactivity.this, "Success--->" +utils.genprojectpath () +resetphotoname,0). Show (); Empty the text box Metphotoname.settext (""); Metmodel.settext (""); Dialog.cancel (); } }); Builder.show (); } break; Default:super.onActivityResult (Requestcode, ResultCode, data); Break } }
Picture shows:
/** * Image Display * @param uri */private void Processpicture (Uri uri) {final string[] projection = {Media Store.Images.Media.DATA}; Final cursor cursor = managedquery (URI, projection, NULL, NULL, NULL); Cursor.movetofirst (); Final int columnindex = Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA); String ImagePath = cursor.getstring (columnindex); LOG.E ("Mainactivity", ImagePath); Final String TargetPath = Bitmaputils.toregularhashcode (ImagePath) + ". jpg"; Bitmaputils.compressbitmap (ImagePath, TargetPath, 640); Compression bitmap = Bitmaputils.decodebitmap (ImagePath, 150); Decomposition of Mimage.setimagebitmap (bitmap); Display//Upload server//final avatarurlupdaterequest request = new Avatarurlupdaterequest (request_id_avatar_url_updat E,mfilename,targetpath,mloginresult); Final Loginrequest request = new Loginrequest (0, "[email protected]", "123456"); GenericdaTamanager.retrievedata (request, this); }
Source: http://download.csdn.net/detail/gao_chun/8128717
Android take pictures and save (compress, decompose)