從assets中讀取文本和圖片資源
/* 從assets 文件夾中讀取文本數據 /
public static String getTextFromAssets(final Context context, String fileName) {
String result = "";
try {
InputStream in = context.getResources().getAssets().open(fileName);
// 獲取文件的字節數
int lenght = in.available();
// 創建byte數組
byte[] buffer = new byte[lenght];
// 將文件中的數據讀到byte數組中
in.read(buffer);
result = EncodingUtils.getString(buffer, "UTF-8");
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/** 從assets 文件夾中讀取圖片 */
public static Drawable loadImageFromAsserts(final Context ctx, String fileName) {
try {
InputStream is = ctx.getResources().getAssets().open(fileName);
return Drawable.createFromStream(is, null);
} catch (IOException e) {
if (e != null) {
e.printStackTrace();
}
} catch (OutOfMemoryError e) {
if (e != null) {
e.printStackTrace();
}
} catch (Exception e) {
if (e != null) {
e.printStackTrace();
}
}
return null;
}</pre>
本文由用戶 xg48 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!