Android:画像を読み込んでみる

Androidで画像を読み込む方法はいくつかある

ファイルパスを指定:BitmapFactoryを使用

SDカードから画像を読み込んでみる。

String  sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
Bitmap image = BitmapFactory.decodeFile(sdPath+"/Pictures/hoge.png");


Uri指定:MediaStoreを使用

ContentPrividerを通した時にUri指定となり、ファイルパスと少々違ったやり方が必要になる。

ContentResolver cr = getContentResolver();
Bitmap image = MediaStore.Images.Media.getBitmap( cr, uri );

超簡単でお手軽。
ContentResolverが必要なので場合によっては面倒かもね。

Uri指定:BitmapFactoryを使用


InputStream  in = cr.openInputStream(uri);
Bitmap image = BitmapFactory.decodeStream(in);
in.close();

decodeStreamにBitmap.Optionsを付けられるメソッドもあるので、細かい指定が可能そうだ。


コメント

このブログの人気の投稿

nginxでlocalhostとしてアクセスをサーバーに転送する方法

Android・・・テキスト描画あれこれ, ascent(), descent()等

Android:stateに応じて切り替わるdrawable・・・StateListDrawable