2011/02/28

[Android Dev.] 다양한 방법으로 Bitmap 불러오기

출처: http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/2/

setContentView(R.layout.main);        

// Load from file
//*
ImageView image = (ImageView) findViewById(R.id.ivImage);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/myimage/android.png");
image.setImageBitmap(bMap);
//*/

// Load from Stream
//*
ImageView image = (ImageView) findViewById(R.id.ivImage);

FileInputStream in;
BufferedInputStream buf;
try
{
in = new FileInputStream("/sdcard/myimage/android.png");
buf = new BufferedInputStream(in);
Bitmap bMap = BitmapFactory.decodeStream(buf);
image.setImageBitmap(bMap);
if (in != null)
{
in.close();
}
if (buf != null)
{
buf.close();
}
}
catch (Exception e)
{
Log.e("Error reading file", e.toString());
}
//*/

// Load from resource
//*
ImageView image = (ImageView) findViewById(R.id.ivImage);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.android);
image.setImageBitmap(bMap);
//*/

// Load from Bitmaps
//*
ImageView image = (ImageView) findViewById(R.id.ivImage);
FileInputStream in;
BufferedInputStream buf;
try
{
in = new FileInputStream("/sdcard/myimage/android.png");
buf = new BufferedInputStream(in);
byte[] bMapArray= new byte[buf.available()];
buf.read(bMapArray);
Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
image.setImageBitmap(bMap);
if (in != null)
{
in.close();
}
if (buf != null)
{
buf.close();
}
}
catch (Exception e)
{
Log.e("Error reading file", e.toString());
}
//*/

No comments :

Post a Comment