assets に画像を置きそれを取り込む場合
ImageViewで表示する画像がassetsに入っているケースです。
画像をassetsに入れたいのですがデフォルトでは無いので作ります。
![imageview a01b - [Android] ImageView 画像を表示させる3つの方法 imageview a01b - [Android] ImageView 画像を表示させる3つの方法](img/imageview2-/imageview_a01b.png)
下の動画のように、assets folder を作ってから入れます。
「app」右クリック「New」「Folder」「Assets Folder」
![imageview 02 300x226 - [Android] ImageView 画像を表示させる3つの方法 imageview 02 300x226 - [Android] ImageView 画像を表示させる3つの方法](img/imageview2-/imageview_02-300x226.png)
そのまま「Finish」
![imageview 03 300x277 - [Android] ImageView 画像を表示させる3つの方法 imageview 03 300x277 - [Android] ImageView 画像を表示させる3つの方法](img/imageview2-/imageview_03-300x277.png)
できた assets に画像 img_3.jpg をコピー&ペーストします。
プロジェクトの階層内に入り、フォルダに直接入れることもできます。
..¥app¥src¥main¥assets¥img_3.jpg
assets からはファイルの読み出しはtry catchを使って例外処理をするようにします。
これは例外が発生した場合でもアプリが回復できる余地を作ることになります。
また、リソース開放漏れが無いようにfinalyでclose()を入れるか、try-with-resourcesを使います。
|
|
try{ ... } catch{ ... }finally { close(); } |
これで画像を取り込む記述は InputStream とAssetsManagerを使って このようにします。
|
|
ImageView imageView3 = findViewById(R.id.image_view_3); AssetManager assets = getResources().getAssets(); // try-with-resources try (InputStream istream = assets.open("img_3.jpg")){ Bitmap bitmap = BitmapFactory.decodeStream(istream); imageView3.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } |
レイアウトは、指定されたID(image_view_3)を使います
|
|
<ImageView android:id="@+id/image_view_3" android:scaleType="centerCrop" android:layout_width="wrap_content" android:layout_height="wrap_content" /> |
まとめると
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
import android.content.res.AssetManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import java.io.InputStream; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); ImageView imageView3 = findViewById(R.id.image_view_3); AssetManager assets = getResources().getAssets(); // try-with-resources try (InputStream istream = assets.open("img_3.jpg")){ Bitmap bitmap = BitmapFactory.decodeStream(istream); imageView3.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } } } |
activity_main.xml
|
|
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:id="@+id/image_view_3" android:scaleType="centerCrop" android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="@string/img_description3" /> </android.support.constraint.ConstraintLayout> |
strings.xml
|
|
<resources> ... <string name="img_description3">image3</string> </resources> |
画像が表示されたでしょうか
![imgeview 04 - [Android] ImageView 画像を表示させる3つの方法 imgeview 04 - [Android] ImageView 画像を表示させる3つの方法](img/imageview2-/imgeview_04.jpg)
drawable VS assets
なぜdrawableだけでなくassetsを使うかというと、
- アイコン画像などを100個以上使うようなケースでassetsではフォルダ分けができます。drawableではベタで放り込むしかありません。
- assetsからの取り込みの場合はBitmapFactoryを使って画像をダウンサンプリリングすることができるためdrawableよりは比較的大きいファイルを取り込めます。
画像サイズというのはファイルサイズではなく、画像の横 x 縦のサイズになります。なぜならbitmap変換するところがネックなので。
メモリのリミットはAndroidの場合、端末依存なのでこれで大丈夫とは言い切れないのも困ったことで、Android2.x系までカバーする場合は512x512pix程度に抑えた方がいいという噂もあります。
ただ、大きいサイズの画像取り込みに関しては、後述のライブラリーPicassoを使うことである程度可能です。
Picasso ライブラリー
今さらではありますが、Picasso – Square Open Source というライブラリーがあります。これを使うと例えば5760×3760サイズの画像も表示することができます。Picasso 大きい画像を扱ってみる
![picasso 01 - [Android] ImageView 画像を表示させる3つの方法 picasso 01 - [Android] ImageView 画像を表示させる3つの方法](img/imageview2-/picasso_01.png)
5760×3760の画像
関連:
References:
ImageView | Android Developers
Picasso – Square Open Source