Android gets the resource image under asset

Source: Internet
Author: User


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + TWFpbkFjdGl2aXR5yOfPwjo8L3A + CjxwPjxwcmUgY2xhc3M9 "brush: java;"> package cc. testasset; import java. io. inputStream; import android. OS. bundle; import android. app. activity; import android. content. res. assetManager; import android. graphics. bitmap; import android. graphics. bitmapFactory;/*** Demo Description: * Get the resource image under asset ** Note: * 1 you cannot obtain the absolute path of a resource in a folder under asset. * Because asset is to be packaged into the apk * 2 some people say: * String filePath = "File: // android_asset/file name"; * indicates the path of a file. after testing, this method is unreliable. * See the code. */public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {// Test 1: Obtain asset resources. try {AssetManager assetManager = getAssets (); InputStream is = assetManager. open ("Fresh_01.jpg"); // The Code commented out below is unreliable. if used, there will be exceptions/ /InputStream is = assetManager. open ("file: // android_asset/Fresh_01.jpg"); Bitmap bitmap = BitmapFactory. decodeStream (is); if (bitmap! = Null) {System. out. println ("Test 1: width =" + bitmap. getWidth () + ", height =" + bitmap. getHeight ();} else {System. out. println ("bitmap = null") ;}} catch (Exception e) {System. out. println ("exception information:" + e. toString ();} System. out. println ("============================"); // Test 2: obtain the image resources in a folder in asset. try {AssetManager assetManager = getAssets (); InputStream is = assetManager. open ("ml_lszn_Fresh/Fresh_02.jpg"); Bitmap bitma P = BitmapFactory. decodeStream (is); if (bitmap! = Null) {System. out. println ("Test 2: width =" + bitmap. getWidth () + ", height =" + bitmap. getHeight ();} else {System. out. println ("bitmap = null") ;}} catch (Exception e) {System. out. println ("exception information:" + e. toString ();} System. out. println ("============================"); // test III: traverse all image resources in a folder in asset. try {InputStream is = null; Bitmap bitmap = null; String dirPath = "ml_lszn_Fresh"; String photoName = null; AssetManager assetManager = getAssets (); // use the list () method to obtain the names of all objects in a folder. String [] photos = assetManager. list (dirPath); for (int I = 0; I <photos. length; I ++) {photoName = photos [I]; // The complete path of a file in the dirPath + "/" + photoName group is = assetManager. open (dirPath + "/" + photoName); bitmap = BitmapFactory. decodeStream (is); System. out. println ("Test 3: I =" + I + ", width =" + bitmap. getWidth () + ", height =" + bitmap. getHeight () ;}} catch (Exception e) {System. out. println ("exception information:" + e. toString ());}}}
Main. xml is as follows:

     
  
 



Related Article

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.