ExifInterface获得了不受支持的图像

时间:2017-10-19 07:10:25

标签: android

我在Android应用中使用了publc class Classb{ 来获取图像的相机方向。我的代码适用于ExifInterface。但是当构建版本大于或等于24时,它会在android studio logcat中发出警告并且无法正常工作。

这是来自onActivityResult方法的代码块:

Build.VERSION.SDK_INT < 24

当我从相机中选择图像时,这是logcat结果:

if(requestCode == REQUEST_CAPTURE_IMG && resultCode == RESULT_OK) {
            Log.d(TAG, "Inside camera operation");
            int reqWidth = 480, reqHeight = 800;
            try {
                InputStream inStream = null;
                try {
                    inStream = getContentResolver().openInputStream(imageUri);

                    //Decode image size
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;

                    BitmapFactory.decodeStream(inStream, null, options);
                    inStream.close();

                    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

                    options.inJustDecodeBounds = false;
                    inStream = getContentResolver().openInputStream(imageUri);

                    CommonStaticClass.mImage = BitmapFactory.decodeStream(inStream, null, options);

                    ExifInterface exif = null;

                    try {
                        //File pictureFile = new File(imgDecodableString);
                        if (Build.VERSION.SDK_INT >= 24) {
                            exif = new ExifInterface(inStream);
                            Log.d("exif", "sdk 24");
                        }
                        else {
                            exif = new ExifInterface(imageUri.getPath());
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    int orientation = ExifInterface.ORIENTATION_NORMAL;

                    if (exif != null)
                        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

                    switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            CommonStaticClass.mImage = rotateBitmap(CommonStaticClass.mImage, 90);
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            CommonStaticClass.mImage = rotateBitmap(CommonStaticClass.mImage, 180);
                            break;

                        case ExifInterface.ORIENTATION_ROTATE_270:
                            CommonStaticClass.mImage = rotateBitmap(CommonStaticClass.mImage, 270);
                            break;
                    }

                    inStream.close();
                } catch (IOException e) {
                    //Toast.makeText(this, "IO exception", Toast.LENGTH_SHORT).show();
                    Toast.makeText(this, SelectSuitActivity.this.getString(R.string.wrong_msg), Toast.LENGTH_SHORT).show();
                }
                //CommonStaticClass.mImage = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
                startActivity(intent);
            } catch (Exception e) {
                Toast.makeText(this, SelectSuitActivity.this.getString(R.string.wrong_msg), Toast.LENGTH_SHORT).show();
            }
        }

如果有人能够弄清楚这里有什么问题会很有帮助。谢谢。

3 个答案:

答案 0 :(得分:2)

在BitmapFactory使用之后使用inStream。这是不可能的。再次打开流。

答案 1 :(得分:0)

在我的情况下,由于屏幕截图的文件路径而收到此警告,我已经为应用程序截图了,该文件的完整路径如下:

/storage/emulated/0/DCIM/Screenshots/Screenshot_2019-10-10-12-16-54-246_com.some_package_name.png

如您所见,有。因此,在com和some_package_name 之间。我收到此错误。

答案 2 :(得分:0)

无效图片:ExifInterface 获得了不受支持的图片格式文件(ExifInterface 仅支持 JPEG 和某些 RAW 图像格式)或损坏的 JPEG 文件到 ExifInterface。

此错误显示在我的应用程序中,可能是扩展名问题在图像 URL 中不正确。我使用 Glide 库加载图像并显示错误,例如 ExifInterface 仅支持 JPEG 和某些 RAW 图像格式

之后我从应用程序设置中清除了缓存和数据,然后它显示了正确的图像,但它显示了如下不同的日志消息

Load failed for http://myserverurl.com/web/image/product.template/7783/image_1920?unique=20201014051228 with size [146x156]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There was 1 cause:
java.io.FileNotFoundException(https://myserverurl.com/web/image/product.template/7783/image_1920?unique=20201014051228)
 call GlideException#logRootCauses(String) for more detail
  Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE

在 Glide 中使用的图片 URL 下方 https://myserverurl.com/web/image/product.template/7783/image_1920?unique=20201014051228

注意:以上图片网址仅供参考,目前在该服务器中不可用。