2012年10月21日 星期日

Android take picture, crop picture, select picture by using intent

Take Picture
public static Uri takePhoto( Activity act )
 {
  //Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  //act.startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
  
  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  Uri mImageCaptureUri = Uri.fromFile( new File( Environment.getExternalStorageDirectory(), "tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg") );
  
  cameraIntent.putExtra( android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri );
  
  try
  {
   cameraIntent.putExtra( "return-data", true);
   act.startActivityForResult(cameraIntent, PICK_FROM_CAMERA);
  }
  catch( ActivityNotFoundException e )
  {
   
  }
  
  return mImageCaptureUri;
 
 }
Crop Picture
@Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
      
      if ( resultCode != RESULT_OK )
      {
       File f = new File( _imageCaptureUri.getPath() );
       if ( f.exists() )
       {
        f.delete();
       }
       
       return;
      }
      
      switch ( requestCode )
      {
      case CameraCaptureAPI.CROP_FROM_CAMERA:
       
       final Bundle extras = data.getExtras();
       
       if ( extras != null )
       {
        final Bitmap photo = extras.getParcelable("data");
        
         AlertDialog.Builder alert = new AlertDialog.Builder(this);                 
         alert.setTitle( getString( R.string.confirm ) );  

          alert.setPositiveButton( R.string.confirm, new DialogInterface.OnClickListener() {  
             public void onClick(DialogInterface dialog, int whichButton) {  
              
              PictureUploadAPI.uploadPicture( photo, ProfileViewActivity.this );
             }           
               
           });  

           alert.setNegativeButton( R.string.cancel, new DialogInterface.OnClickListener() {

                 public void onClick(DialogInterface dialog, int which) {
                     // TODO Auto-generated method stub
                        
                 }
             });
                     
             alert.show();
       
       }
       
       File f = new File( _imageCaptureUri.getPath() );
       if ( f.exists() )
       {
        f.delete();
       }
      
       break;
      
      case CameraCaptureAPI.PICK_FROM_FILE:
      Intent intent = new Intent( "com.android.camera.action.CROP" );
      _imageCaptureUri = data.getData();
      intent.setDataAndType( _imageCaptureUri, "image/*"); //!!! important
      intent.putExtra("outputX", 96);
      intent.putExtra("outputY", 96);
      intent.putExtra("aspectX", 1);
      intent.putExtra("aspectY", 1);
      intent.putExtra("scale", true);
      intent.putExtra("return-data", true);
      
      try
      {
         startActivityForResult(intent, CameraCaptureAPI.CROP_FROM_CAMERA);
      }
      catch ( Exception e )
      {
       Log.d("pick picture", e.getMessage());
      }
      
      break;
      
      case CameraCaptureAPI.PICK_FROM_CAMERA:
      Intent intent1 = new Intent( "com.android.camera.action.CROP" );
      intent1.setDataAndType(_imageCaptureUri, "image/*"); //!!! important
      intent1.putExtra("outputX", 96);
      intent1.putExtra("outputY", 96);
      intent1.putExtra("aspectX", 1);
      intent1.putExtra("aspectY", 1);
      intent1.putExtra("scale", true);
      intent1.putExtra("return-data", true);
      
      try
      {
         startActivityForResult(intent1, CameraCaptureAPI.CROP_FROM_CAMERA);
      }
      catch ( Exception e )
      {
       Log.d("pick picture", e.getMessage());
      }
      
      break;
      }
     }
Select Picture
 Intent intent = new Intent( Intent.ACTION_GET_CONTENT );
 intent.setType( "image/*" );
           
 try{
      startActivityForResult( intent, CameraCaptureAPI.PICK_FROM_FILE );
 }
 catch( Exception e )
 {
     Log.d("browse picture", e.getMessage());
 }

http://www.technotalkative.com/android-launch-native-camera-application-to-capture-image-and-use-it-inside-imageview/

沒有留言:

張貼留言