Created
November 8, 2012 01:22
-
-
Save yujiro/4035808 to your computer and use it in GitHub Desktop.
ギャラリーから画像を選択して表示するだけのアプリ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.superarai.android.sample.imageview; | |
| import java.io.BufferedInputStream; | |
| import android.app.Activity; | |
| import android.content.Intent; | |
| import android.graphics.Bitmap; | |
| import android.graphics.BitmapFactory; | |
| import android.os.Bundle; | |
| import android.view.Menu; | |
| import android.widget.ImageView; | |
| /** | |
| * ギャラリーから画像を選択して表示するだけのアプリ | |
| * 参考: http://wiki.livedoor.jp/moonlight_aska/d/%A5%AE%A5%E3%A5%E9%A5%EA%A1%BC%A4%AB%A4%E9%B2%E8%C1%FC%A4%F2%BC%E8%C6%C0%A4%B9%A4%EB | |
| * (上のURLのソースを大体そのまま使ってる) | |
| * @author arai | |
| * | |
| */ | |
| public class MainActivity extends Activity { | |
| private static final int REQUEST_GALLERY = 0; | |
| private ImageView imageView; | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| imageView = (ImageView)findViewById(R.id.imageView1); | |
| Intent intent = new Intent(); | |
| intent.setType("image/*"); | |
| intent.setAction(Intent.ACTION_GET_CONTENT); | |
| startActivityForResult(intent, REQUEST_GALLERY); | |
| } | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| getMenuInflater().inflate(R.menu.activity_main, menu); | |
| return true; | |
| } | |
| @Override | |
| protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| if (requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) { | |
| try { | |
| BufferedInputStream inputStream = new BufferedInputStream(getContentResolver().openInputStream(data.getData())); | |
| Bitmap image = BitmapFactory.decodeStream(inputStream); | |
| imageView.setImageBitmap(image); | |
| } catch (Exception e) { | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment