public class Utils {
public static List<Message> getMesages() {
//File file = new File("file:///android_asset/helloworld.txtv");
AssetManager assetManager = getAssets();
InputStream ims assetManager.open("helloworld.txt");
}
}
想讀出assets文件夾下的資源。當使用File時顯示fileNotFound異常,但是使用AssetManager getAssets()方法時,又不能識別。請問有什麼方法可以解決這個問題呢?
public void getGetAssets(){
AssetManager assetManager= context.getAssets();
Log.e(TAG, assetManager.getLocales()+"");
try {
InputStream inputStream = assetManager.open("atest");
inputStream.read();
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(isr);
Log.i(TAG,"inputStream :" + inputStream.read());
Log.i(TAG,"BufferedReader :" +br);
Log.i(TAG,"BufferedReader :" +br.toString());
Log.i(TAG,"BufferedReader :" +new String(br.readLine()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
你只要保證你的assets文件下有這個“atest”文件就可以了,我試了不會報錯的。我的測試文件內容就是aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
09-21 03:04:30.626: E/bai(833): [Ljava.lang.String;@41072288
09-21 03:04:30.626: I/bai(833): inputStream :97
09-21 03:04:30.635: I/bai(833): BufferedReader :java.io.BufferedReader@41075e98
09-21 03:04:30.635: I/bai(833): BufferedReader :java.io.BufferedReader@41075e98
09-21 03:04:30.635: I/bai(833): BufferedReader :aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa