[Java] View Plaincopyprint?
- <span style="FONT-SIZE:18PX;" > PackageCom.demo;
- Import Java.io.BufferedOutputStream;
- Import Java.io.File;
- Import java.io.FileNotFoundException;
- Import Java.io.FileOutputStream;
- Import java.io.IOException;
- Import Java.util.Calendar;
- Import Java.util.Locale;
- Import android.app.Activity;
- Import android.content.Intent;
- Import Android.graphics.Bitmap;
- Import Android.os.Bundle;
- Import android.os.Environment;
- Import Android.provider.MediaStore;
- Import Android.text.format.DateFormat;
- Import Android.util.Log;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.Button;
- Import Android.widget.ImageView;
- Import Android.widget.Toast;
- Public class Mycameraactivity extends Activity {
- /** Called when the activity is first created. * /
- Private button button;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Button = (button) Findviewbyid (R.id.button);
- Button.setonclicklistener (new Onclicklistener () {
- @Override
- public void OnClick (View v) {
- //TODO auto-generated method stub
- Intent Intent = new Intent (mediastore.action_image_capture);
- Startactivityforresult (Intent, 1);
- }
- });
- }
- @Override
- protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
- //TODO auto-generated method stub
- Super.onactivityresult (Requestcode, ResultCode, data);
- if (ResultCode = = ACTIVITY.RESULT_OK) {
- String sdstatus = Environment.getexternalstoragestate ();
- if (!sdstatus.equals (environment.media_mounted)) { //Detect if SD is available
- LOG.I ("Testfile",
- "SD card isn't avaiable/writeable right now.");
- return;
- }
- String name = new DateFormat (). Format ("Yyyymmdd_hhmmss", Calendar.getinstance (Locale.china)) + ". jpg";
- Toast.maketext (this, name, Toast.length_long). Show ();
- Bundle bundle = Data.getextras ();
- Bitmap Bitmap = (Bitmap) bundle.get ("data"); Get the data returned by the camera and convert it to bitmap picture format
- FileOutputStream B = null;
- //??????????????????????????????? Why not directly saved in the System album location???????????????????
- File File = new file ("/sdcard/myimage/");
- File.mkdirs (); //Create a folder
- String fileName = "/sdcard/myimage/" +name;
- try {
- b = new FileOutputStream (FileName);
- Bitmap.compress (Bitmap.CompressFormat.JPEG, N, b); Writing data to a file
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- } finally {
- try {
- B.flush ();
- B.close ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- ((ImageView) Findviewbyid (R.id.imageview)). Setimagebitmap (bitmap); //Display the picture in ImageView
- }
- }
- }
- </span>
- Previous Android call system picture browser and return picture path
- Next import Project: Some items are hidden because they are in the workspace directory
-
Top
Call the Android system camera to take pictures and save