使用相机打算拍照没有SD卡 [英] Using the camera intent to take a picture without an sdcard

查看:247
本文介绍了使用相机打算拍照没有SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用相机时没有SD卡present遇到了麻烦。

I'm having trouble using the camera when there's no sdcard present.

当有一个SD卡,在使用相机是微不足道的,例如

When there is an sdcard, using the camera is trivial, e.g.

http://www.vogella.com/articles/AndroidCamera/article.html +一个其他的例子太多了。

http://www.vogella.com/articles/AndroidCamera/article.html + a plethora of other examples.

不过,我需要让我的应用程序提供给那些没有SD卡,设备(如索尼的Xperia系列)。我已经试过修改code,这样我使用的内部存储(我认为):

However, I need to make my app available to devices which don't have SD cards, (e.g. the Sony Xperia series.) I've tried modifying the code such that I'm using the internal storage (I think):

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

然而,在结果是:

However, upon result:

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

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

...

位图为null。

这使我相信,有一些权限问题....也许?

Which leads me to believe that there's some permissions issue....maybe?

我已经尝试了一些其他的内部存储选项, http://developer.android.com/guide/topics/data/data-storage.html#filesInternal 例如: getFilesDir(),但相同的结果:空位图

I've tried some of the other internal storage options, http://developer.android.com/guide/topics/data/data-storage.html#filesInternal e.g. getFilesDir() but the same result: null bitmap.

有没有人在使用相机没有SD卡有过任何成功的?

Has anyone had any success in using the camera without an sdcard?

推荐答案

试试这个。它的工作原理。

Try this. It works..

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

            }
        }
    }
}

这篇关于使用相机打算拍照没有SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆