
在建立檔案時會有的裝置無法解出圖形(目前所知Android4.2以上的版本不會出錯),而以file1/🏆💯表示
程式在執行的時候便出現:java.io.FileNotFoundException: /storage/sdcard0/TestFile/file1/🏆💯.txt: open failed: EINVAL (Invalid argument)
↓↓↓↓↓↓來看看程式碼的部份↓↓↓↓↓↓
String backupFolder = Environment.getExternalStorageDirectory() +"/TestFile/";
String fileName = "file1/🏆💯";
// 當fileName內含/會crash
int searchSeparator = fileName.indexOf("/");
if (searchSeparator != -1) {
Log.d("tsots","檔名包含//字元, 要去除");
fileName = fileName.replaceAll("/", "");
}
// 當fileName內含🏆會crash
int searchInvalid1 = fileName.indexOf("🏆");
if(searchInvalid1!= -1){
Log.d("tsots","檔名包含🏆字元, 要去除");
fileName = fileName.replaceAll("🏆", "");
}
// 當fileName內含💯會crash
int searchInvalid2 = fileName.indexOf("💯");
if(searchInvalid2!= -1){
Log.d("tsots","檔名包含💯字元, 要去除");
fileName = fileName.replaceAll("💯", "");
}
String txtPath = backupFolder + fileName + ".txt";//文檔路徑
Log.d("tsots","備份於"+txtPath);
File chatLog = new File(txtPath);
if(!chatLog.exists()) {
Log.d("tsots","路徑之前不存在, 新建立");
chatLog.getParentFile().mkdirs();
try {
Log.d("tsots","檔案之前不存在, 新建立");
chatLog.createNewFile();
} catch (IOException e) {
Log.e("tsots", e);
}
}
//若沒有過濾掉/🏆💯就會在這行發生如標題的錯誤訊息
BufferedWriter bfwriter = new BufferedWriter(new FileWriter(txtPath), 1024);
﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍﹍
而實機測試發現
Samsung S3 無法顯示這兩個圖, 會以空白表示
HTC ONE SV 可以顯示這兩個圖
Sony Experia 可以顯示這兩個圖
沒有留言:
張貼留言