1

没有 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卡的情况下使用相机成功?

4

1 回答 1

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();

            }
        }
    }
}
于 2013-02-22T08:50:05.570 回答