android 读取 Assets 文件

 // 读取Assets txt文件
1
StringBuffer content = new StringBuffer(); 2 InputStream is = null; 3 is = getAssets().open("files/***.txt"); 4 5 if (is != null) { 6 InputStreamReader inputreader = new InputStreamReader(is); 7 BufferedReader buffreader = new BufferedReader(inputreader); 8 String line; 9 // 分行读取 10 while ((line = buffreader.readLine()) != null) { 11 content.append(line); 12 content.append("\n"); 13 } 14 is.close(); 15 }

 读取并显示图片:

//获取asset下某个文件夹中的图片资源
   try {
      AssetManager assetManager = getAssets();
      InputStream is = assetManager.open("ml_lszn_Fresh/Fresh_02.jpg");
      Bitmap bitmap = BitmapFactory.decodeStream(is);
      if (bitmap != null) {
        System.out.println("width=" + bitmap.getWidth() + " ,height="+ bitmap.getHeight());
      } else {
        System.out.println("bitmap == null");
      }
   } catch (Exception e) {
      System.out.println("异常信息:" + e.toString());
   }

 

// 遍历asset下某个文件夹中的所有图片资源
   try {
      InputStream is=null;
      Bitmap bitmap=null;
      String dirPath="ml_lszn_Fresh";
      String photoName=null;
      AssetManager assetManager = getAssets();
      //使用list()方法获取某文件夹下所有文件的名字
      String [] photos=assetManager.list(dirPath);
      for (int i = 0; i < photos.length; i++) {
        photoName=photos[i];
        //利用dirPath+"/"+photoName组拼某文件完整路径
        is = assetManager.open(dirPath+"/"+photoName);
        bitmap = BitmapFactory.decodeStream(is);
        System.out.println(" i="+i+" ,width=" + bitmap.getWidth() + " ,height="+ bitmap.getHeight());
      }
   } catch (Exception e) {
      System.out.println("异常信息:" + e.toString());
   }

 

posted @ 2015-06-28 10:53  晕菜一员  阅读(905)  评论(0)    收藏  举报