Image resources for reading different locations (Drawable,asset,sdcard) in Android

Source: Internet
Author: User

Way One:

You have saved the picture to the drawable directory and obtained drawable or bitmap with the picture ID, which is most commonly used. (If you only know the name of the picture, you can also get the image ID by the name of the picture)

(1) Get drawable by Image ID

Drawable Drawable=getresource (). getdrawable (R.DRAWABLE.XXX);

(2) Get bitmap by Image ID

Resource Res=gerresource ();

Bitmap Bitmap=bitmapfactory.decoderesource (res, id);

(3) Get the ID of the picture by the name of the picture (two methods)

int ID =res.getidentifier (name, Deftype, defpackage); Name: The name of the picture, Deftype: Resource type (drawable,string ... ), Defpackage: Project's package name

Drawable Drawable=getresource (). getdrawable (ID);


Way two:

Saved the picture to the Assest directory, know the name of the picture, get the picture by InputStream Drawabl

or Bitmap.

Assetmanager ASM=GETASSETMG ();

InputStream Is=asm.open (name);//name: Name of the picture

(1) Get drawable
drawable da = Drawable.createfromstream (is, null);

(2) Get bitmap
Bitmap Bitmap=bitmapfactory.decodestream (IS);


Way three: Picture saved in SDcard, through the path of the picture H

/Picture Path
String Imgfilepath = Environment.getexternalstoragedirectory (). ToString ()
+ "/dcim/device.png";

(1) file input stream

FIS = new FileInputStream (new file (Imgfilepath));//File input stream

Bitmap bmp = Bitmapfactory.decodestream (FIS);

(2)

ImageView IV = (ImageView) Findviewbyid (r.id.image);
Bitmap bit = Bitmapfactory.decodefile ("/sdcard/android.bmp");
Iv.setimagebitmap (bit);


Iv.setimagedrawable (Drawable.createfrompath (New File (Environment.getexternalstoragedirectory (), "camera.jpg"). GetAbsolutePath ()));

Image resources for reading different locations (Drawable,asset,sdcard) in Android

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.