隨筆-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 閱讀(2522) 評論(0)  編輯  收藏 所屬分類: JavaAndroid
          主站蜘蛛池模板: 廊坊市| 和平区| 苗栗市| 石门县| 蒲城县| 澳门| 呼和浩特市| 康马县| 浮山县| 铜梁县| 黑河市| 温州市| 红桥区| 南皮县| 板桥市| 通河县| 二连浩特市| 吉林市| 高密市| 汽车| 阿克陶县| 清涧县| 禹城市| 台安县| 札达县| 大洼县| 弥渡县| 东宁县| 永昌县| 东莞市| 桃园县| 嘉兴市| 福泉市| 惠州市| 宣化县| 通州市| 姜堰市| 云梦县| 夏津县| 宿州市| 平阳县|