Android用户上传的图像在ImageView中使用错误的Exif数据旋转

时间:2017-07-15 20:29:09

标签: android image sqlite exif image-rotation

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

@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标签并用矩阵旋转Bitmap,使用光标获取图像信息并旋转,并使用Glide。这三个都失败了,我不知道接下来要做什么。有什么建议吗?

完整的代码可以找到here

更新
根据要求,我added Exif检查代码无济于事。如前所述,方向始终为零,因此我根据链接中的代码得到一个空位图。

1 个答案:

答案 0 :(得分:0)

我不知道saveImage()做了什么。假设它接收Bitmap并返回&#34;路径&#34;,我认为它将Bitmap保存到文件中。如果是这样,那肯定不会起作用,因为Bitmap没有EXIF数据。

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