<喎?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: