2013-02-22 140 views
1

使用相機意圖在沒有SD卡的情況下拍攝照片

當沒有SD卡時,我無法使用相機。

當有SD卡時,使用照相機是微不足道的,例如,

http://www.vogella.com/articles/AndroidCamera/article.html +大量的其他例子。但是,我需要讓我的應用可用於沒有SD卡的設備(例如Sony Xperia系列)。我嘗試修改代碼,以便使用內部存儲器(我認爲):

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
File file = new File(getDir("myDirec", Context.MODE_WORLD_WRITEABLE), "tmp_photo_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); 
file.createNewFile(); 
mImageCaptureUri = Uri.fromFile(file); 

intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri); 
intent.putExtra("return-data", true); 

然而,當結果是:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intentReturn) { 
    if (resultCode != RESULT_OK) 
     return; 

    String path = mImageCaptureUri.getPath(); 
    Bitmap bitmap = BitmapFactory.decodeFile(path); 

... 

bitmap爲空。

這使我相信有一些權限問題....也許?

我試過一些其他的內部存儲選項,例如http://developer.android.com/guide/topics/data/data-storage.html#filesInternal,例如getFilesDir()但結果相同:null bitmap

有沒有人在沒有SD卡的情況下使用相機有任何成功?

回答

2

試試這個。它的工作原理..

private Uri imageUri; 

    public void onClick(View arg0) { 
    switch (arg0.getId()) {  
     case R.id.btnImageCapture: 

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
    File dir = context.getDir("directory", Context.MODE_PRIVATE); 
    File photo = new File(dir, "Pic.jpg"); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, 
      Uri.fromFile(photo)); 
    imageUri = Uri.fromFile(photo); 
    startActivityForResult(intent, OPEN_CAMERA); 
        break; 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
    case OPEN_CAMERA: 
     if (resultCode == Activity.RESULT_OK) { 
      Uri selectedImage = imageUri; 
      getContentResolver().notifyChange(selectedImage, null); 
      ImageView imageView = (ImageView) findViewById(R.id.ImageView); 
      ContentResolver cr = getContentResolver(); 
      Bitmap bitmap; 
      try { 
       bitmap = android.provider.MediaStore.Images.Media 
       .getBitmap(cr, selectedImage); 

       imageView.setImageBitmap(bitmap); 
       Toast.makeText(this, selectedImage.toString(), 
         Toast.LENGTH_LONG).show(); 

      } catch (Exception e) { 
       Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) 
         .show(); 

      } 
     } 
    } 
} 
+0

真棒,謝謝,工作。 – tomblah 2013-02-24 00:55:42

相關問題