2011-05-10 69 views

回答

7
Uri imageUri; 
final int TAKE_PICTURE = 115; 

public void capturePhoto(View view) { 
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
    File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png"); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, 
      Uri.fromFile(photoFile)); 
    imageUri = Uri.fromFile(photoFile); 
    startActivityForResult(intent, TAKE_PICTURE); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
     case TAKE_PICTURE: 
      if (resultCode == Activity.RESULT_OK) { 
       Uri selectedImageUri = imageUri; 
       //Do what ever you want 
     } 
    } 
} 
+0

感謝科坦其有益的給我 – 2011-05-10 05:26:43

+1

太感謝你了,我已經一直在摸索着像一個小時試圖讓相機對象和預覽方法工作,當這一切都是我從一開始就想要的。 – 2011-05-23 19:33:40

+0

嗨阿貝漢,你的代碼將顯示所有第三方相機應用程序以及默認相機應用程序。如果某個人只想顯示默認相機而不顯示任何第三方相機應用程序的選擇器,如** Line Camera **和** Paper Camera **? – 2013-09-27 12:16:29

7

它是用來打開攝像頭的目的是

buttonCapturePhoto.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(intent, CAPTURE_IMAGE); 
     } 
    }); 

,讓你拍攝後的圖像的代碼是

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    Uri uriImage; 
    InputStream inputStream = null; 
    if ((requestCode == SELECT_IMAGE || requestCode == CAPTURE_IMAGE) && resultCode == Activity.RESULT_OK) { 
     uriImage = data.getData(); 
     try { 
      inputStream = getContentResolver().openInputStream(uriImage); 
      Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options); 
      imageView.setImageBitmap(bitmap); 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
     imageView.setAdjustViewBounds(true); 
    } 
} 

0

這是一個簡單的例子。總之,這會將圖像作爲一個小的位圖返回。如果要檢索全尺寸的圖像,會稍微複雜一些。

ImageView takePhotoView = (ImageView) findViewById(R.id.iwTakePicture); 
Bitmap imageBitmap = null; 
takePhotoView.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub    
       dispatchTakePictureIntent(0); 
      } 
     }); 

    private void dispatchTakePictureIntent(int actionCode) { 
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
      startActivityForResult(takePictureIntent, actionCode);  
     } 

    private void handleSmallCameraPhoto(Intent intent) { 
     Bundle extras = intent.getExtras(); 
     this.imageBitmap = (Bitmap) extras.get("data"); 
     takePhotoView.setImageBitmap(imageBitmap); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(resultCode == RESULT_OK) 
      handleSmallCameraPhoto(data); 
    } 
相關問題