随笔-348  评论-598  文章-0  trackbacks-0

 

String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator + "tmpPhoto.jpg"
Intent takePictureFromCameraIntent 
= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(
new   File(SD_CARD_TEMP_DIR))); 
startActivityForResult(takePictureFromCameraIntent, TAKE_PICTURE_WITH_CAMERA); 

public void onActivityResult(int requestCode, int resultCode, Intent data) {        
super.onActivityResult(requestCode, resultCode, data);        
//      Picture taken from camera        
  if (requestCode == TAKE_PICTURE_WITH_CAMERA) {                
     
if (resultCode == Activity.RESULT_OK) {                       
       
// http://code.google.com/p/android/issues/detail?id=1480                        
       
//Toast.makeText(AddPhotos.this, "" + data, Toast.LENGTH_SHORT).show();                       
        
// on activity return                       
         File f = new File(SD_CARD_TEMP_DIR);                       
          
try {                            
                Uri capturedImage 
=     Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(),  f.getAbsolutePath(), nullnull));                               
                 Log.i(
"camera""Selected image: " + capturedImage.toString());                            
                 f.delete();                        
                 }
 
         
catch (FileNotFoundException e) {                            
         
// TODO Auto-generated catch block                           
          e.printStackTrace();                        
          }
               
           }
                
      
else {                        
      Log.i(
"Camera""Result code was " + resultCode);                
      }
        
      }
            
      }
 


http://code.google.com/p/android/issues/detail?id=1480
讨论的很有意义
Well, now on 1.5, the documentation has been updated: "If the EXTRA_OUTPUT is not
present, then a small sized image is returned as a Bitmap object in the extra field.
If the EXTRA_OUTPUT is present, then the full-sized image will be written to
the Uri value of EXTRA_OUTPUT."

But using the following piece of code:

    private void takePic2() {
        Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(FILE_PATH)));
        startActivityForResult(imageCaptureIntent, 1);
    }
The doc says:
1/ "If the EXTRA_OUTPUT is not present, then a small sized image is returned"
2/ "If the EXTRA_OUTPUT is present, then the *full-sized* image will be written"

Alas 2/ is not true, it produces a 512x384 image - that is not "full sized".

private void doTakePhotoAction() {    
    
// http://hfoss.org/Tutorial:Camera_and_Gallery_Demo    
    
// http://stackoverflow.com/questions/1050297/how-to-get-the-url-of-the-captured-image    
    
// http://www.damonkohler.com/2009/02/android-recipes.html    
    
// http://www.firstclown.us/tag/android/    
    
// The one I used to get everything working: http://groups.google.com/group/android-developers/msg/2ab62c12ee99ba30     
    
    Intent intent 
= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
    
    
//Wysie_Soh: Create path for temp file    
    mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),    
                        
"tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));    
    
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);    
    
    
try {    
        intent.putExtra(
"return-data"true);    
        startActivityForResult(intent, PICK_FROM_CAMERA);    
    }
 catch (ActivityNotFoundException e) {    
        
//Do nothing for now    
    }
    
}
    
    
protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
    
if (resultCode != RESULT_OK) {    
        
return;    
    }
    
    
    
switch (requestCode) {    
    
    
case CROP_FROM_CAMERA: {    
        
//Wysie_Soh: After a picture is taken, it will go to PICK_FROM_CAMERA, which will then come here    
        
//after the image is cropped.    
    
        
final Bundle extras = data.getExtras();    
    
        
if (extras != null{    
            Bitmap photo 
= extras.getParcelable("data");    
    
            mPhoto 
= photo;    
            mPhotoChanged 
= true;    
            mPhotoImageView.setImageBitmap(photo);    
            setPhotoPresent(
true);    
        }
    
    
        
//Wysie_Soh: Delete the temporary file                            
        File f = new File(mImageCaptureUri.getPath());                
        
if (f.exists()) {    
            f.delete();    
        }
    
    
        InputMethodManager mgr 
= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);    
        mgr.showSoftInput(mPhotoImageView, InputMethodManager.SHOW_IMPLICIT);    
    
        
break;    
    }
    
    
    
case PICK_FROM_CAMERA: {    
        
//Wysie_Soh: After an image is taken and saved to the location of mImageCaptureUri, come here    
        
//and load the crop editor, with the necessary parameters (96x96, 1:1 ratio)    
    
        Intent intent 
= new Intent("com.android.camera.action.CROP");    
        intent.setClassName(
"com.android.camera""com.android.camera.CropImage");    
    
        intent.setData(mImageCaptureUri);    
        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);                
        startActivityForResult(intent, CROP_FROM_CAMERA);    
    
        
break;    
    
    }
    
    }
    
}
   



---------------------------------------------------------
专注移动开发

Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2010-01-12 17:37 TiGERTiAN 阅读(2509) 评论(0)  编辑  收藏 所属分类: JavaAndroid

只有注册用户登录后才能发表评论。


网站导航: