facebook twitter hatena line google mixi email

Android/写真ギャラリー呼出

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
  • HelloworldActivity.java
import android.widget.ImageView;
private static final int REQUEST_GALLERY = 0;
ImageView imgView;
imgView = (ImageView)findViewById(R.id.testtest);
// ギャラリー呼び出し
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(intent, REQUEST_GALLERY);
// startActivity(intent);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if(requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {
        try {
            InputStream in = getContentResolver().openInputStream(data.getData());
            Bitmap img = BitmapFactory.decodeStream(in);
            in.close();
            // 選択した画像を表示
            imgView.setImageBitmap(img);
        } catch (Exception e) {
        }
    }
}

main.xml

<ImageView
  android:id="@+id/testtest"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:src="@drawable/ic_launcher"
  android:scaleType="fitStart"
  android:adjustViewBounds="true" />