Help us understand the problem. What is going on with this article?

ギャラリーから取得した写真を正しい向きで表示

More than 5 years have passed since last update.

アルバムにある写真に加工するプログラムを作るために、
ギャラリーから画像を取得する
を試したら、簡単に取得できた!
が、なぜか全て横向き表示・・・。
調べてみると、写真の向きが機種によって違い、向きを指定する必要があるらしい。

で、
ギャラリーから呼び出す画像の向きを取得する
を参考に向きを取得。
ExifInterfaceを使ったサンプルを記載しているサイトはいろいろあったが、画像ファイルの場所がわかっている前提のばかりで、ギャラリーから取得した画像のファイル名を取得しているのはここくらい。

向きがわかったので、
Exif情報を利用して、写真の向きを正しく表示する
を参考に、縮尺なども加えてできたのがこれ。

package jp.murapon.sample;

import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {
    private static final int REQUEST_GALLERY = 0;
    private ImageView imgView;
    private float viewWidth;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imgView = (ImageView)findViewById(R.id.imgview_id);
        // ギャラリー呼び出し
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, REQUEST_GALLERY);

        // ウィンドウマネージャのインスタンス取得
        WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
        // ディスプレイのインスタンス生成
        Display disp = wm.getDefaultDisplay();
        viewWidth = disp.getWidth();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {
            try {
                InputStream in = getContentResolver().openInputStream(data.getData());
                Bitmap bitmap = BitmapFactory.decodeStream(in);
                in.close();

                //dataからfilepathへの変換  
                String[] columns = {MediaStore.Images.Media.DATA };  
                Cursor c = getContentResolver().query(data.getData(), columns, null, null, null);  
                c.moveToFirst();  
                ExifInterface exifInterface = new ExifInterface(c.getString(0));
                // 向きを取得
                int orientation = Integer.parseInt(exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION));

                imgView.setScaleType(ScaleType.MATRIX);
                imgView.setImageBitmap(bitmap);

                // 画像の幅、高さを取得
                int wOrg = bitmap.getWidth();
                int hOrg = bitmap.getHeight();
                imgView.getLayoutParams();
                LayoutParams lp = (LayoutParams) imgView.getLayoutParams();

                float factor;
                Matrix mat = new Matrix();
                mat.reset();
                switch(orientation){
                    case 1://only scaling
                        factor = (float)viewWidth/(float)wOrg;
                        mat.preScale(factor, factor);
                        lp.width = (int)(wOrg*factor);
                        lp.height = (int)(hOrg*factor);
                        break;
                    case 2://flip vertical
                        factor = (float)viewWidth/(float)wOrg;
                        mat.postScale(factor, -factor);
                        mat.postTranslate(0, hOrg*factor);
                        lp.width = (int)(wOrg*factor);
                        lp.height = (int)(hOrg*factor);
                        break;
                    case 3://rotate 180
                        mat.postRotate(180, wOrg/2f, hOrg/2f);
                        factor = (float)viewWidth/(float)wOrg;
                        mat.postScale(factor, factor);
                        lp.width = (int)(wOrg*factor);
                        lp.height = (int)(hOrg*factor);
                        break;
                    case 4://flip horizontal
                        factor = (float)viewWidth/(float)wOrg;
                        mat.postScale(-factor, factor);
                        mat.postTranslate(wOrg*factor, 0);
                        lp.width = (int)(wOrg*factor);
                        lp.height = (int)(hOrg*factor);
                        break;
                    case 5://flip vertical rotate270
                        mat.postRotate(270, 0, 0);
                        factor = (float)viewWidth/(float)hOrg;
                        mat.postScale(factor, -factor);
                        lp.width = (int)(hOrg*factor);
                        lp.height = (int)(wOrg*factor);
                        break;
                    case 6://rotate 90
                        mat.postRotate(90, 0, 0);
                        factor = (float)viewWidth/(float)hOrg;
                        mat.postScale(factor, factor);
                        mat.postTranslate(hOrg*factor, 0);
                        lp.width = (int)(hOrg*factor);
                        lp.height = (int)(wOrg*factor);
                        break;
                    case 7://flip vertical, rotate 90 
                        mat.postRotate(90, 0, 0);
                        factor = (float)viewWidth/(float)hOrg;
                        mat.postScale(factor, -factor);
                        mat.postTranslate(hOrg*factor, wOrg*factor);
                        lp.width = (int)(hOrg*factor);
                        lp.height = (int)(wOrg*factor);
                        break;
                    case 8://rotate 270
                        mat.postRotate(270, 0, 0);
                        factor = (float)viewWidth/(float)hOrg;
                        mat.postScale(factor, factor);
                        mat.postTranslate(0, wOrg*factor);
                        lp.width = (int)(hOrg*factor);
                        lp.height = (int)(wOrg*factor);
                        break;
                }
                imgView.setLayoutParams(lp);
                imgView.setImageMatrix(mat);
                imgView.invalidate();
            } catch (Exception e) {
                Log.e(e.toString(), "Error");
            }
        } 
    }
}
murapon
yumemi
みんなが知ってるあのサービス、実はゆめみが作ってます。スマホアプリ/Webサービスの企画・UX/UI設計、開発運用。Swift, Kotlin, PHP, Vue.js, React.js, Node.js, AWS等エンジニア・クリエイターの会社です。Twitterで情報配信中https://twitter.com/yumemiinc
http://www.yumemi.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした