【问题标题】:save image from bytes returned from android camera intent is saved as small image从android相机意图返回的字节中保存图像被保存为小图像
【发布时间】:2012-07-30 21:11:48
【问题描述】:

我正在尝试使用 Android Camera Intent 捕获图像。相机意图返回字节数组,当我将字节数组保存为位图时,我得到一个非常小的图像,而不是根据当前相机设置(当前在 android 移动相机中设置 1024 个像素)获取图像。

通常我会从相机意图获取文件路径,但不知何故我没有从这个设备获取,所以我从相机意图返回的字节创建位图。

任何人都知道这是为什么以及如何解决此问题。谢谢。

下面是我正在使用的java代码块。

private Intent cameraIntent = null;

public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);

cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

}

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

if (requestCode == CAMERA_PIC_REQUEST) {  
    if (resultCode == RESULT_OK) {
      if ( data != null)
        {
           Bitmap myImage = null;
           Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
           ByteArrayOutputStream stream = new ByteArrayOutputStream();
           imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100,stream);
           byte[] byteArray = stream.toByteArray();
           BitmapFactory.Options options = new BitmapFactory.Options();
           myImage = BitmapFactory.decodeByteArray(byteArray, 0,byteArray.length, options);
           fileOutputStream = new FileOutputStream(sPath);
           BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
           myImage.compress(CompressFormat.JPEG, 100, bos);
           bos.flush();
           bos.close();
        }
    }
}
}

【问题讨论】:

    标签: android android-intent camera


    【解决方案1】:

    data.getExtras("data") 仅返回缩略图。要获得完整尺寸的图像,您需要将相机意图传递给存储该图像并稍后检索的文件。下面是一个粗略的例子。

    开始意图:

    File dir = new File(Environment.getExternalStorageDirectory() + "/dcim/myappname");
    File mFile = File.createTempFile("myImage", ".png", dir);
    
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
    

    内部onActivityResult

    if (resultCode == RESULT_OK) {
       Bitmap bm = BitmapFactory.decodeFile(mFile.getAbsolutePath());
    }
    // do whatever you need with the Bitmap
    

    请记住,mFile 必须是全局的或以某种方式持久化,以便在必要时可以调用它。

    【讨论】:

      猜你喜欢
      • 2012-12-20
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      相关资源
      最近更新 更多