From: http://www.oschina.net/code/snippet_4873_4437
1. You have saved the image to the drawable directory and obtained the drawable using the image ID.
Resource res = gerresource ();
Drawable = res. getdrawable (ID); // ID is R. drawable. Image name
2. Obtain bitmap by image ID
Resource res = gerresource ();
Bitmap bitmap = bitmapfactory. decoderesource (Res, ID );
3. If you only know the image name, you can obtain the image ID through the image name.
// Name: image name deftype: Image type (PNG, JPEG), defpackage: project package name
Int id = int id = res. getidentifier (name, deftype, defpackage );
// After obtaining the ID, you can obtain bitmap or drawable based on your needs.
4. You have saved the image to the assest directory.
// Know the image name and open the image through inputstream
Assetmanager ASM = getassetmg ();
Inputstream is = ASM. Open (name); // name: image name
// Obtain drawable
Drawable da = drawable. createfromstream (is, null );
// Obtain bitmap
Bitmap bitmap = bitmapfactory. decodestream (is );
4. Save the image to sdcard. The path of the image is known.
// Image path
String Path = environment. getexternalstoragedirectory (). tostring () + "/dcim/device.png ";
Randomaccessfile mminithumbfile;
File imgfile = new file (PATH );
Try {
Mminithumbfile = new randomaccessfile (imgfile, "RW ");
} Catch (ioexception ex ){
// Open as read-only so we can at least read the existing
// Thumbnails.
Try {
Mminithumbfile = new randomaccessfile (imgfile, "R ");
} Catch (ioexception ex2 ){
// Ignore exception
System. Out. println (ex2.tostring ());
}
}
Data = new byte [2, 10553];
Try {
Mminithumbfile. Seek (0 );
Int got = mminithumbfile. Read (data, 0, 10552 );
System. Out. println ("got =" + got );
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
System. Out. println (E. tostring ());
}
If (Data! = NULL ){
// Obtain bitmap through data
Bitmap bitmap = bitmapfactory. decodebytearray (data, 0, Data. Length );