Android:使用BitmapFactory.decodeFile打开的文件方向错误

时间:2012-08-14 19:45:27

标签: android resize exif bitmapfactory

标题很简单,使用BitmapFactory.decodeFile打开的文件在ImageView上显示时方向错误。从摄像头捕获并保存在tmp文件中的图像,如果设备有返回data.getData()的bug,则我至少有一个对该文件的引用。

这只是启动相机活动并捕获图像文件

private void startCamera() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    if (hasImageCaptureBug()) {
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Constants.TMPFILE_PATH)));
    } else {
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    }
    startActivityForResult(intent, CAMERA_PIC_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        if (resultCode == RESULT_OK) {
            Uri uri = null;

            if (hasImageCaptureBug()) {
                File f = new File(Constants.TMPFILE_PATH);
                try {
                    uri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), f.getAbsolutePath(), null, null));
                } catch (FileNotFoundException e) {

                }
            } else {
                uri = data.getData();
            }

            imageFilePath = Image.getPath(this, uri);

            if (Image.exists(imageFilePath)) {
                ImageView image = (ImageView) findViewById(R.id.thumbnail);
                int targetW     = (int) getResources().getDimension(R.dimen.thumbnail_screen_width);
                int degrees     = (int) Image.getRotation(this, uri);

                Bitmap bmp = Image.resize(imageFilePath, targetW);
                bmp = Image.rotate(bmp, degrees);

                image.setAdjustViewBounds(true);
                image.setImageBitmap(bmp);
            }
        }
    }
}

此文件调整图像大小

public class Image {
    public static Bitmap resize(String pathName, int targetW) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;

        Bitmap bmp = BitmapFactory.decodeFile(pathName, opts);

        int photoW = opts.outWidth;
        int photoH = opts.outHeight;
        int targetH = Math.round((photoH * targetW) / photoW);
        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

        opts.inJustDecodeBounds = false;
        opts.inSampleSize = scaleFactor;
        opts.inPurgeable = true;

        bmp = BitmapFactory.decodeFile(pathName, opts);

        return bmp;
    }
}

尝试获取ExifOrientation,但始终为0,因为文件本身正确定向,当我加载文件时,文件显示方向错误。

此致

1 个答案:

答案 0 :(得分:0)

似乎我的预览图像的问题是Constants.TMPFILE_PATH,图像没有保存在那里,我只是使用这个修复Display the latest picture taken in the image view layout in android !,但如果我把它发布到服务器上,问题仍然存在...我会将此作为回答检查并向此开启一个新问题......


被修改

要解决此问题,只需重构新映像,然后将其上传到服务器,因为文件本身的原始数据的exif方向错误。