android - ExifInterface 始终返回 0 方向

标签 android picasso android-bitmap

我看到很多与此相关的问题,但我没有找到问题的答案。

我尝试显示可以使用相机捕获或从存储加载的个人资料图像。

我使用 ExifInterface 来确定加载图像的 Picasso 的正确旋转。

我不明白为什么所有图片都有一个方向 = 0

enter image description here

下面是我的代码,非常简单:

 private void onCaptureImageResult(Intent data) {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);


    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".jpg");

    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    try {
        Picasso.with(getBaseContext()).load("file:" + destination.getPath()).rotate(MyTools.getFileExifRotation("file:" + destination.getPath())).into(avatar);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {

    Bitmap bm=null;
    Uri uri=null;
    if (data != null) {
        try {
            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
            uri=data.getData();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    try {
        Picasso.with(getBaseContext()).load("file:" + uri.getPath()).rotate(MyTools.getFileExifRotation("file:" + uri.getPath())).into(avatar);
    } catch (IOException e) {
        e.printStackTrace();
    }
}


 public static int getFileExifRotation(String path) throws IOException {
    ExifInterface exifInterface = new ExifInterface(path);
    int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            return 90;
        case ExifInterface.ORIENTATION_ROTATE_180:
            return 180;
        case ExifInterface.ORIENTATION_ROTATE_270:
            return 270;
        default:
            return 0;
    }
}

我在 LG G4 手机上进行测试。

最佳答案

您有一个位图。位图不包含方向。之后,将位图压缩为 .jpg。这不会添加方向或 exif header 。

因此对于该 .jpg 使用 ExifInterface 是没有用的。或者尝试确定方向。

你知道你正在保存缩略图!?请使用原始版本。

关于android - ExifInterface 始终返回 0 方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38309622/

相关文章:

android - 网上取图出现 "Bitmap is not valid"错误如何解决

java - BottomNavigationView 在单击时不起作用 - 必须单击两次

android - Android webview中资源请求的请求头中key 'X-requested-with'的值如何修改,而不是页面请求?

android - 内存全是图片,可能是Bitmap.compress(format, int, stream)引起的

android - picasso 图像加载库 : how to load video thumbnails

android - 为什么我的 BitmapFactory.decodeByteArray 返回 null?

android - 如何在 Android Studio 中使用不同的模拟器

android - 在 Picasso Android 中实现 "withDelay"(用于略读)

android - Picasso 加载本 map 片速度慢?

android - View 中的 ImageView setImageBitmap()