ImageView讀取Assets資料夾圖片(Image From Assets Folder)

1.讀取Assets資料夾內的檔案只需要在程式裡(.java)撰寫即可。
public class MainActivity extends Activity
{
private ImageView img;
private Button btn;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//顯示Assets資料夾圖片的ImageView與進行讀取Assets資料夾圖片的Button
img = (ImageView) findViewById(R.id.img);
btn = (Button) findViewById(R.id.assets);

btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
//帶入Assets資料夾內的圖片路徑(Assets資料夾: android資料夾/black圖片)
img.setImageBitmap(getBitmapFromAssets("android/black.png"));
}
});
}

//讀取Assets資料夾圖片,型態為Bitmap
private Bitmap getBitmapFromAssets(String file)
{
try
{
AssetManager am = getAssets();
InputStream is = am.open(file);
return bitmap;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
}

source: http://dean-android.blogspot.hk/2013/06/androidimageviewassetsimage-from-assets.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s