31
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ギャラリーから画像のURIとBitmapを取得する色んな方法

Last updated at Posted at 2016-08-23

普段の生活で撮った写真を取得したい場合が多いので、まとめてみました。
取得方法は2種類。端末のギャラリーから取得するか、暗黙的Intentでギャラリーアプリから取得するか。

画像URIの取得

端末標準のギャラリーから取得する場合

MainActivity.java
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);

intent.setType("image/*");
startActivityForResult(intent, READ_REQUEST_CODE);

暗黙的Intentでギャラリーアプリから取得する場合

MainActivity.java


Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

intent.setType("image/*");
startActivityForResult(intent, READ_REQUEST_CODE);

実際の画像(Bitmap)の取得

画像のURIを取得したら、今度はBitmapがほしいですよね。
場合によってはREAD_EXTERNAL_STORAGEのpermissionが必要となるので注意してください。

MainActivity.java
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
  if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
      Uri uri = null;
      if (resultData != null) {
          uri = resultData.getData();
          try {
              Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
          }catch (IOException e) {
                    e.printStackTrace();
                }
            }
      }
  }

実装例

MainActivity.java
public class MainActivity extends AppCompatActivity {
    private static final int READ_REQUEST_CODE = 42;
    ImageView imageView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        imageView = (ImageView)findViewById(R.id.imageView);

        button1.setOnClickListener(new View.OnClickListener() {
            //端末標準ギャラリーを起動し、画像のURIを取得
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");

                startActivityForResult(intent, READ_REQUEST_CODE);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            //暗黙的Intentを使ってギャラリーアプリを選択し、画像のURIを取得
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");

                startActivityForResult(intent, READ_REQUEST_CODE);
            }
        });


    }
    @Override
    public void onActivityResult(int requestCode, int resultCode,
                                 Intent resultData) {

        if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            Uri uri = null;
            if (resultData != null) {
                uri = resultData.getData();
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                    imageView.setImageBitmap(bitmap);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


31
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?