Android 访问assets下的文件

assets下经常可以放一些比较大的资源,对于这些资源我们如何访问。

步骤

1.获取AssetManager。 AssetManager am = getResources().getAssets(); 2.利用AssetManager的open(String filePath)方法打开对应的输入流。 InputStream is = am.open(assetsFileName);

读取图片文件保存到SD卡示例代码public boolean saveToSDCard(String localFilePath, String fileName,Bitmap bitmap) {String extraPath = Environment.getExternalStorageDirectory().toString();// 使用绝对路径extraPath = extraPath + “http://blog.csdn.net/” + “PaperCut”+”http://blog.csdn.net/”+localFilePath;// String path = null;File file = new File(extraPath);if (!file.exists())file.mkdirs();try {filePath = file.getPath() + “http://blog.csdn.net/” + fileName + “.png”;File newFile = new File(filePath);if (newFile.exists()) {Toast.makeText(getApplicationContext(), R.string.finishTips1, // 已经保存过啦亲Toast.LENGTH_SHORT).show();return true;}else {FileOutputStream fileOutputStream = new FileOutputStream(filePath);bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);// 另一种格式的结尾// bitmap.compress(CompressFormat.PNG, 50, fileOutputStream);fileOutputStream.flush();fileOutputStream.close();}} catch (Exception e) {e.printStackTrace();;}return true;}private Bitmap getImageFromAssetsFile(int position){//Bitmap image = null;Bitmap image = null;String assetsFileName = null;assetsFileName = “album/”+is.imageMyChooseName.get(position)+”/1.png”;//AssetManager从assets文件夹中获取资源AssetManager am = getResources().getAssets();try{InputStream is = am.open(assetsFileName);//从InputStream解码生成imageimage = BitmapFactory.decodeStream(is);is.close();}catch (IOException e){e.printStackTrace();}return image;}//调用saveToSDCard(“album”,”filePath”),getImageFromAssetsFile(position));

,发光并非太阳的专利,你也可以发光

Android 访问assets下的文件

相关文章:

你感兴趣的文章:

标签云: