diff --git a/app/src/main/java/qianmu/container/activity/H5/WebViewActivity.java b/app/src/main/java/qianmu/container/activity/H5/WebViewActivity.java index e05a5ca..1b52be9 100644 --- a/app/src/main/java/qianmu/container/activity/H5/WebViewActivity.java +++ b/app/src/main/java/qianmu/container/activity/H5/WebViewActivity.java @@ -357,8 +357,15 @@ public class WebViewActivity extends BaseActivity { @Override protected void onDestroy() { - super.onDestroy(); LoggerUtil.e(TAG, "onDestroy()"); + if (binding.web != null) { + binding.web.stopLoading(); + binding.web.clearHistory(); + binding.web.removeAllViews(); + binding.web.destroy(); + } + if (handler != null) handler.removeCallbacksAndMessages(null); + super.onDestroy(); } private void loadH5Url(boolean loadUrl) { diff --git a/app/src/main/java/qianmu/container/util/FileUtil.java b/app/src/main/java/qianmu/container/util/FileUtil.java index cce080e..71cae97 100644 --- a/app/src/main/java/qianmu/container/util/FileUtil.java +++ b/app/src/main/java/qianmu/container/util/FileUtil.java @@ -305,23 +305,22 @@ public class FileUtil { * @return */ public static byte[] file2byte(String filePath) { - byte[] buffer = null; - try { - File file = new File(filePath); - FileInputStream fis = new FileInputStream(file); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - byte[] b = new byte[1024]; + File file = new File(filePath); + if (!file.exists() || !file.isFile()) { + return null; + } + try (FileInputStream fis = new FileInputStream(file); + ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length())) { + byte[] buffer = new byte[8192]; // 8KB 更合理 int n; - while ((n = fis.read(b)) != -1) { - bos.write(b, 0, n); + while ((n = fis.read(buffer)) != -1) { + bos.write(buffer, 0, n); } - fis.close(); - bos.close(); - buffer = bos.toByteArray(); - } catch (Throwable t) { - LoggerUtil.e("file2byte", StringUtil.getThrowableStr(t)); + return bos.toByteArray(); + } catch (IOException e) { + LoggerUtil.e("file2byte", StringUtil.getThrowableStr(e)); + return null; } - return buffer; } @@ -413,7 +412,6 @@ public class FileUtil { if (!file.exists()) file.createNewFile(); FileWriter fileWriter = new FileWriter(filePath, append); fileWriter.write(content); - if (fileWriter == null) return; fileWriter.flush(); fileWriter.close(); } catch (Throwable t) { @@ -579,21 +577,24 @@ public class FileUtil { /** * 读取本地JSON,显示汉字正确,txt文件设置时UTF-8,UNIX * */ - public static String readLocalJsonForStorage(Context context, String fileName){ - - String resultString=""; - - try { - File file = new File(fileName); - InputStream inputStream=new FileInputStream(file); - byte[] buffer=new byte[inputStream.available()]; - inputStream.read(buffer); - resultString=new String(buffer,"GB2312"); - inputStream.close(); + public static String readLocalJsonForStorage(Context context, String fileName) { + File file = new File(fileName); + if (!file.exists()) { + return null; + } + StringBuilder sb = new StringBuilder(); + try (InputStream is = new FileInputStream(file); + InputStreamReader isr = new InputStreamReader(is, "UTF-8"); + BufferedReader br = new BufferedReader(isr)) { + String line; + while ((line = br.readLine()) != null) { + sb.append(line); + } } catch (Exception e) { - // TODO: handle exception + e.printStackTrace(); // 至少打日志 + return null; } - return resultString; - + return sb.toString(); } + }