前言:閑來無事窘俺,寫了個小工具類熊泵,是關(guān)于獲取Assets文件的相關(guān)操作瘦赫,內(nèi)容和簡單:
- 加載assets目錄下的網(wǎng)頁,返回一個路徑胃珍;
- 加載assets目錄下的圖片資源梁肿;
- 加載assets目錄下文本文件蜓陌;
- 加載assets目錄下音樂。
/**
* @desc
* @auth 方毅超
* @time 2018/1/23 9:36
*/
public class AssetsUtil {
private AssetsUtil() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 加載assets目錄下的網(wǎng)頁,返回一個路徑
* webView.loadUrl("file:///android_asset/html/index.htmll");
*
* @param htmlFileName 帶后綴 如 index.html
* @return
*/
public static String getHtml(String htmlFileName) {
return "file:///android_asset/html/" + htmlFileName;
}
/**
* 加載assets目錄下的圖片資源
*
* @param ctx
* @param fileName 帶后綴
* @return
*/
public static Bitmap getBitmap(Context ctx, String fileName) {
InputStream is = null;
try {
is = ctx.getResources().getAssets().open(fileName);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
/**
* 加載assets目錄下文本文件
*
* @param ctx
* @param fileName 帶后綴
* @return
*/
public static String getFile(Context ctx, String fileName) {
String result = "";
try {
InputStream in = ctx.getResources().getAssets().open(fileName);
// 獲取文件的字節(jié)數(shù)
int lenght = in.available();
// 創(chuàng)建byte數(shù)組
byte[] buffer = new byte[lenght];
// 將文件中的數(shù)據(jù)讀到byte數(shù)組中
in.read(buffer);
result = new String(buffer, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 加載assets目錄下音樂
*
* @param ctx
* @param musicFileName 帶后綴
*/
public static void openMusic(Context ctx, String musicFileName) {
AssetFileDescriptor afd = null;
MediaPlayer mPlayer = new MediaPlayer();
try {
// 打開指定音樂文件,獲取assets目錄下指定文件的AssetFileDescriptor對象
afd = ctx.getResources().getAssets().openFd(musicFileName);
mPlayer.reset();
// 使用MediaPlayer加載指定的聲音文件栈雳。
mPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
// 準備聲音
mPlayer.prepare();
// 播放
mPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 從assets目錄中復(fù)制整個文件夾內(nèi)容
*
* @param context Context 使用CopyFiles類的Activity
* @param oldPath String 原文件路徑 如:/aa
* @param newPath String 復(fù)制后路徑 如:xx:/bb/cc
*/
public static void copyFilesFassets(Context context, String oldPath, String newPath) {
try {
String fileNames[] = context.getAssets().list(oldPath);//獲取assets目錄下的所有文件及目錄名
if (fileNames.length > 0) {//如果是目錄
File file = new File(newPath);
file.mkdirs();//如果文件夾不存在护奈,則遞歸
for (String fileName : fileNames) {
copyFilesFassets(context, oldPath + "/" + fileName, newPath + "/" + fileName);
}
} else {//如果是文件
InputStream is = context.getAssets().open(oldPath);
FileOutputStream fos = new FileOutputStream(new File(newPath));
byte[] buffer = new byte[1024];
int byteCount = 0;
while ((byteCount = is.read(buffer)) != -1) {//循環(huán)從輸入流讀取 buffer字節(jié)
fos.write(buffer, 0, byteCount);//將讀取的輸入流寫入到輸出流
}
fos.flush();//刷新緩沖區(qū)
is.close();
fos.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//如果捕捉到錯誤則通知UI線程
// MainActivity.handler.sendEmptyMessage(COPY_FALSE);
}
}
}