【问题标题】:Android Camera Intent woesAndroid相机意图问题
【发布时间】:2011-01-18 02:30:25
【问题描述】:

希望有人可以提供一些指示(或正确的答案)... 简单的应用程序,使用内置的相机应用程序拍摄图像,将图像保存到单独的应用程序。完成。

问题:相机应用程序将图像保存在默认应用程序位置(/mnt/sdcard/external_sd/DCIM/Camera)以及我的自定义路径(在下面的代码中)。 除了文件名之外,这两个文件完全相同。 external_sd 文件(我想要的那个)用破折号(-)保存,而我的自定义文件路径用下划线保存。文件大小完全相同。

我怎样才能停止这种双重图像问题? 我缺少一个额外的意图选项吗? 还是我这样做完全错了,错过了什么? 我使用的是 Galaxy S Vibrant。

代码sn-p:

private static Uri _outputFileUri;
private static File _file;
private ImageView _image;
private SimpleDateFormat _simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); 


    _takePicture = (Button) findViewById(R.id.takePicture);
    _takePicture.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            _intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);          

            _file = new File(Environment.getExternalStorageDirectory() +
                            "/Android/data/my own folder/files/",
                             _simpleDateFormat.format(new Date()).toString() + 
                             ".jpg");

            _outputFileUri = Uri.fromFile(_file);

            _intent.putExtra(MediaStore.EXTRA_OUTPUT, _outputFileUri);
            startActivityForResult(_intent, CAMERA_ACTIVITY);
        }
    });   


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_CANCELED) {
        Toast.makeText(this, "Activity cancelled", Toast.LENGTH_LONG).show();
        return;
    }

    switch (requestCode) {  

        case CAMERA_ACTIVITY: 

            if (resultCode == RESULT_OK) {

                try{
                    Bitmap b = MediaStore.Images.Media.getBitmap(getContentResolver(), _outputFileUri);
                    _image.setImageBitmap(b);
                    _image.invalidate();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
            break;
    }
}

【问题讨论】:

    标签: android android-camera


    【解决方案1】:

    这是与设备相关的行为。我的观察是 HTC 设备没有这个重复问题,但三星设备有。

    【讨论】:

      【解决方案2】:

      请删除以下行:

      _file = new File(Environment.getExternalStorageDirectory() +
                                  "/Android/data/my own folder/files/",
                                   _simpleDateFormat.format(new Date()).toString() + 
                                   ".jpg");
      
                  _outputFileUri = Uri.fromFile(_file);
      
                  _intent.putExtra(MediaStore.EXTRA_OUTPUT, _outputFileUri);
      

      同时更新代码以从意图中获取图像:

      Bitmap b = (Bitmap) data.getExtras().get("data");
      _image.setImageBitmap(b);
      _image.invalidate();
      

      这种方式图片不会保存在 sd 卡或默认位置。

      【讨论】:

      • 是的,我已经尝试过,类似的。问题是 1) 位图 b = (Bitmap) data.getExtras().get("data");只返回一个小的缩小的砂砾图像(又名缩略图)和 2)全尺寸正常图像仍保存在 external_sd 默认位置。
      • 我在模拟器中测试过,它不保存图像。图片尺寸问题,好像是bug,请参考post
      【解决方案3】:

      我遇到了同样的问题并放弃了。一段时间后,我发现我不再得到它,我不确定我对我的代码做了什么更改,但我认为这是 MediaStore 的错(查看我未解决的问题:Weird camera Intent behavior

      既然你已经有了图片的URI,为什么不用它来设置ImageViews的位图呢?

      // void setImageURI(Uri uri)
       _image.setImageBitmap(_outputFileUri);
      

      【讨论】:

        【解决方案4】:

        我遇到了这个问题,我是这样解决的:

        File createImageFile() throws IOException{
        
            String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String filename = "IMG_"+timestamp+"_";
            File image = File.createTempFile(filename,".jpg",mGalleryFolder );
            if (image.length() == 0 ){
                boolean delete = image.delete();
            }
            mLocation = image.getAbsolutePath();
            return image;
        }
        

        这不是完全解决问题,但对我有用;)

        【讨论】:

          猜你喜欢
          • 2012-06-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-11
          • 2011-02-13
          相关资源
          最近更新 更多