从相机拍摄的android图片非常小

时间:2011-09-06 15:21:49

标签: android android-camera android-camera-intent

我试图从相机捕获图像,压缩它,然后将其存储到SD卡。如果我使用下面的代码直接将其保存到SD卡,我会得到完整的图像。但是,如果我尝试在系统中加载图像,我会得到一个超小的图像尺寸,如320 x 240而不是完整的5 mp图像。

        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
    startActivityForResult(intent, CAMERA_PIC_REQUEST);  

其中getImageURI()是

    private Uri getImageUri() {
    // Store image in dcim
    file = new File(Environment.getExternalStorageDirectory() + "/DCIM","itshelp.jpg");
    imgUri = Uri.fromFile(file);
    file.deleteOnExit();
       Log.e("syS","file is at "+imgUri);
    return imgUri;
}

现在当我尝试将其保存到内部存储器时,我使用以下代码来获取微小的图像:

    public void imageFromCamera() { //code to retrieve image
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    startActivityForResult(intent, CAMERA_PIC_REQUEST);  
    }

在我的startActivitForResult中我有以下内容:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
        Bitmap bm = (Bitmap) data.getExtras().get("data"); 
        Log.e("sys", "width is "+bm.getWidth()); //im gettting an extremely small width here

        OutputStream outStream = null;
        file = new File(Environment.getExternalStorageDirectory() + "/DCIM","itshelp.png");
        try {
         outStream = new FileOutputStream(file);
         bm.compress(Bitmap.CompressFormat.JPEG, 70, outStream);
         outStream.flush();
         outStream.close();

         Log.i("Hub", "OK, Image Saved to SD");
         Log.i("Hub", "height = "+ bm.getHeight() + ", width = " + bm.getWidth());

        } catch (FileNotFoundException e) {

         e.printStackTrace();
         Log.i("Hub", "FileNotFoundException: "+ e.toString());

        } catch (IOException e) {

         e.printStackTrace();
         Log.i("Hub", "IOException: "+ e.toString());
        }



    }

1 个答案:

答案 0 :(得分:4)

当您在没有MediaStore.EXTRA_OUTPUT的情况下调用相机意图时,相机仅返回一个小缩略图,因为意图包不是为了传递大内存块而设计的。 来自SDK文档for MediaStore

可以发送标准意图操作,让相机应用程序捕获图像并将其返回。 调用者可以传递额外的EXTRA_OUTPUT来控制该图像的写入位置。如果EXTRA_OUTPUT不存在,则在额外字段中返回小尺寸图像作为Bitmap对象。这对于只需要小图像的应用程序非常有用。如果EXTRA_OUTPUT存在,那么完整尺寸的图像将被写入Uri值EXTRA_OUTPUT