gpt4 book ai didi

android - ExifInterface 获取了不受支持的图像

转载 作者:行者123 更新时间:2023-12-02 10:02:14 27 4
gpt4 key购买 nike

我用过ExifInterface在 Android 应用程序中用于获取图像的相机方向。我的代码完美适用于 Build.VERSION.SDK_INT < 24 。但是当构建版本大于或等于 24 时,它会在 android studio logcat 中发出警告并且无法正常工作。

这是我的 onActivityResult 方法的代码块:

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

这是我从相机中选取图像时的 logcat 结果:

    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)

如果有人能找出这里出了什么问题,那将会很有帮助。谢谢。

最佳答案

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

关于android - ExifInterface 获取了不受支持的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46824415/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com