Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExifInterface got an unsupported image

Tags:

android

I have used ExifInterface in an Android app for obtaining camera orientation of an image. My code works perfectly for Build.VERSION.SDK_INT < 24. But when build version is greater or equal to 24 it gives warning in android studio logcat and does not work properly.

Here is my code block from onActivityResult method:

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();
            }
        }

And here is the logcat result when I pick image from camera:

    10-19 12:39:04.399 2912-2912/com.example.myapp I/ExifInterface_JNI: Corrupted image.
    10-19 12:39:04.414 2912-2912/com.example.myapp W/ExifInterface: Invalid image: ExifInterface got an unsupported image format file(ExifInterface supports JPEG and some RAW image formats only) or a corrupted JPEG file to ExifInterface.
    java.io.EOFException
    at java.io.DataInputStream.readByte(DataInputStream.java:270)
    at android.media.ExifInterface.getJpegAttributes(ExifInterface.java:1834)
    at android.media.ExifInterface.loadAttributes(ExifInterface.java:1475)
    at android.media.ExifInterface.<init>(ExifInterface.java:1174)
    at com.lostsym.founder.SelectSuitActivity.onActivityResult(SelectSuitActivity.java:621)
    at android.app.Activity.dispatchActivityResult(Activity.java:6932)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
    at android.app.ActivityThread.-wrap20(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

It would be helpful if anyone can figure out what's wrong here. Thank you.

like image 277
Mazedul Islam Avatar asked Oct 19 '17 07:10

Mazedul Islam


1 Answers

You use inStream after it has been used by BitmapFactory. That is not possible. Open the stream yet another time.

like image 173
greenapps Avatar answered Oct 28 '22 06:10

greenapps