Android用户上传的图像在ImageView中以假Exif数据旋转

问题描述:

我正在开发Android应用程序,其中部分功能是用户可以从其设备中选择图像,将其与列表中的特定项目相关联,以及在应用程序中使用它。我的意图是将图像本身存储在内部存储器中,以及SQLite数据库中图像的路径以及该项目的其余存储信息。
我已经能够让用户选择并且上传图片,但是图片有时会根据图片的拍摄情况进行旋转。我试过看Exif数据,但方向始终为零。我试过用光标作为MediaStore.Images.Media.DATA的信息,但也无济于事。相关的代码的当前状态如下:Android用户上传的图像在ImageView中以假Exif数据旋转

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_details); 

    mDbHelper = new EntryDbHelper(this); 

    Intent intent = getIntent(); 
    pId = intent.getIntExtra(PhrasesActivity.T_KEY, -1); 

    mImageView = (ImageView) findViewById(R.id.details_img); 
    mImageView.setOnLongClickListener(new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 
      //Making an intent to Pick an image, looking for an Internal URI 
      Intent pickImageIntent = new Intent(Intent.ACTION_GET_CONTENT); 
      //Setting the content to images 
      pickImageIntent.setType("image/*"); 
      if(pickImageIntent.resolveActivity(getPackageManager()) != null) { 
       startActivityForResult(pickImageIntent, REQUEST_IMAGE_GET); 
      } 

      return true; 
     } 
    }); 

    setText(); 
    setImage(); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) { 
     Bitmap image = null; 
     Uri imageUri = data.getData(); 

     try { 
      image = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri); 
     } catch(FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch(IOException e) { 
      e.printStackTrace(); 
     } 

     mImageView.setImageBitmap(image); 

     String path = storeImage(image); 

     SQLiteDatabase db = mDbHelper.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(PhraseEntry.COLUMN_URI, path.toString()); 

     String selection = PhraseEntry._ID + " = ?"; 
     String[] selectionArgs = {Integer.toString(pId)}; 

     int count = db.update(PhraseEntry.TABLE_NAME, values, selection, selectionArgs); 

     if(count == 1) { 
      Toast.makeText(this, "Image Updated Successfully", Toast.LENGTH_SHORT).show(); 
     } else { 
      Toast.makeText(this, "Image Updated UNSUCCESSFULLY", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

private void setImage() { 
    SQLiteDatabase db = mDbHelper.getReadableDatabase(); 

    String[] projection = { 
      PhraseEntry.COLUMN_URI 
    }; 

    String selection = PhraseEntry._ID + " = ?"; 
    String[] selectionArgs = {Integer.toString(pId)}; 

    Cursor cursor = db.query(PhraseEntry.TABLE_NAME, projection, selection, selectionArgs, 
      null, null, null); 

    String imagePath = ""; 

    while(cursor.moveToNext()) { 
     imagePath = cursor.getString(
       cursor.getColumnIndexOrThrow(PhraseEntry.COLUMN_URI) 
     ); 
    } 

    if(!imagePath.isEmpty()) { 

     try { 
      File f = new File(imagePath, "phrase_" + Integer.toString(pId)); 
      Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f)); 
      mImageView.setImageBitmap(b); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

    } 
    else { 
     mImageView.setImageResource(R.drawable.sample); 
    } 
    cursor.close(); 
} 

下面是相关布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.bladefrisch.discoveringtalk.DetailsActivity"> 

    <ImageView 
     android:id="@+id/details_img" 
     style="@style/DetailImage" 
     android:contentDescription="Sample" 
     android:src="@drawable/sample" /> 

    <TextView 
     android:id="@+id/details_text" 
     style="@style/DetailText" 
     tools:text="Hello, how are you?" /> 

</LinearLayout> 

这允许用户选择一个图像,将其上传到图像视图(有时旋转),存储图像,并在关闭应用程序后检索图像。轮换是唯一的主要问题,我所尝试的修复并未体现在这里。我在这里引用了太多的答案,但它们分为三大类:检查Exif标签并用矩阵旋转位图,使用光标获取图像信息并旋转,然后使用Glide。三人都失败了,我不知道下一步该做什么。有什么建议么?

完整的代码可以在here找到。

UPDATE
按照要求,我已经added将Exif检查代码无济于事。如前所述,方向始终为零,因此我会根据链接中的代码获取空位图。

+0

“有什么建议吗?” - 问一个问题,你在哪里显示你的“修复[你]已经尝试过”并寻求帮助,提供什么不起作用的具体细节。我们无法帮助您处理我们无法看到的代码。你在这里的代码肯定不起作用,因为它不会旋转图像。 FWIW,[这个示例应用程序](https://github.com/commonsguy/cw-omnibus/tree/v8.6/Camera/EXIFRotater)演示了“检查Exif标签并旋转位图矩阵”解决方案,以及旋转'ImageView'。 – CommonsWare

+0

请参阅[链接](https://stackoverflow.com/questions/14066038/why-does-an-image-captured-using-camera-intent-gets-rotated-on-some-devices-on-a) – santalu

+0

@ CommonsWare我已经添加了我之前尝试过的代码,并根据santalu的链接进行了一些小改动。我已经用结果更新了我的问题。 – BFrisch

我不知道saveImage()做什么。鉴于它需要在Bitmap并返回一个“路径”,我假定它将Bitmap保存到一个文件。如果是这样,那肯定不行,因为Bitmap没有EXIF数据。

使用the support library's edition of ExifInterface的原始内容中读取EXIF标记。使用getContentResolver().openInputStream(imageUri)可获得InputStream,将其传递给ExifInterface构造函数。

+0

这适用于我的设备SDK 24+,因为这是添加了'InputStream'的'ExifInterface'构造函数的地方。 'saveImage()'确实是一个自定义函数,它只是将图像写入内部存储并返回数据库的路径。然后,我的主要问题似乎是访问低于24的设备的'ExifInterface'的原始内容。我可以用不同的API和URI来做到这一点,或者我需要采取不同的方法吗?最低级别的构造函数将一个文件名作为'String',但我想我对这与URI有何不同有点困惑。 – BFrisch

+0

@BFrisch:“这适用于我的设备SDK 24+,因为那是添加了InputStream的ExifInterface构造函数的地方。” - 它也在支持库中,这是我链接到的。 – CommonsWare

+0

我现在明白了。我没有意识到我必须链接到我的Gradle文件中的支持库。一旦我明白支持库是独立的,[https://android-developers.googleblog.com/2016/12/introducing-the-exifinterface-support-library.html?m=1)博客文章是有帮助的。谢谢。 – BFrisch