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/

1 則留言:

  1. I'm on the fence about this, while more customization is good, I have a feeling this is a "in-progress" update, it just feels incomplete and half-way there.
    We use badge layout for apps on design approvals (visual projects), so the image being displayed is important. Old layout "feels like" it had larger images,
    maybe because the images were cropped more loosely so it's easier to tell which project it was at quick glance. Now the image is cropped closer, making it
    harder to scan thru at quick glance. I find myself needing to click into the project more often than usual. Which makes the whole user experience less
    efficient.
    I have a couple suggestions that might make it work better:
    1. Increase the height of the window the cover image is being displayed.
    2. Let us to choose which image to be displayed as "cover" (like how Pinterest handles cover images of each board, was hoping for this for a long time)
    3. Let us adjust which part of the image to show and how tight or loose the crop is (with a fixed window, let us move the image around and maybe enlarge or
    shrink it to control what shows thru the window. Pinterest does a limited form of this, which is very useful in making the cover image relevant)
    4. Allow Cover Image to be ordered in different hierarchy (currently every element can be ordered differently except the Cover Image, it seems to be stuck
    in the 2nd spot, would like the option to set it on another spot in the layout. This one seems like an easy fix, since you guys allow that for every other
    element already)

    回覆刪除