在摄像头意图的活动结果不能在Android 2.36中工作

时间:2013-12-13 09:17:10

标签: android android-intent

我正在尝试使用活动结果从相机获取图像。它在android 4.2上正常工作。但我无法从android 2.36获取图像。

有人可以帮助我吗?

部分源代码附于下方:

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            try {
                if (requestCode == 2) {// image from camera
                    photo = (Bitmap) data.getExtras().get("data");

                    selectedImageUri = data.getData();
                    photo = decodeUri(selectedImageUri);
                    test.setImageBitmap(photo);
}
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(
                    getContentResolver().openInputStream(selectedImage), null, o);

            final int REQUIRED_SIZE = 100;

            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                    break;
                }
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(
                    getContentResolver().openInputStream(selectedImage), null, o2);
        }

2 个答案:

答案 0 :(得分:0)

在我看来,不需要这条线:

photo = (Bitmap) data.getExtras().get("data");

答案 1 :(得分:0)

使用此:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (requestCode == 2) {// image from camera
               if (resultCode == Activity.RESULT_OK) {
            getContentResolver().notifyChange(mUri, null);
            ContentResolver cr = getContentResolver();
            try {
                mPhoto = android.provider.MediaStore.Images.Media
                        .getBitmap(cr, mUri);

                Bitmap useThisBitmap = Bitmap.createScaledBitmap(mPhoto,
                        mPhoto.getWidth(), mPhoto.getHeight(), true);
                test.setImageBitmap(useThisBitmap);

            } catch (Exception e) {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT)
                        .show();
            }
        } else {
            Toast.makeText(this,
                    resultCode + "" + "=" + Activity.RESULT_CANCELED,
                    Toast.LENGTH_SHORT).show();
        }

            }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
相关问题