【问题标题】:Using the camera intent to take a picture without an sdcard使用相机意图在没有 SD 卡的情况下拍照
【发布时间】:2013-02-22 08:33:21
【问题描述】:

当没有 SD 卡时,我无法使用相机。

当有sdcard时,使用相机是微不足道的,例如

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

有没有人在没有sdcard的情况下使用相机成功过?

【问题讨论】:

    标签: java android camera sd-card


    【解决方案1】:

    试试这个。它工作..

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多