Android get image

Source: Internet
Author: User

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.