當字串中包含特殊符號,如
在建立檔案時會有的裝置無法解出圖形(目前所知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 可以顯示這兩個圖
總網頁瀏覽量
基礎Note
☪About Me
(1)
免費軟體
(2)
教學
(4)
教學文件
(42)
會計軟體
(1)
電腦系統
(1)
Adapter
(8)
Adobe Premiere
(1)
AlertDialog
(7)
Android App 介紹
(1)
Animation
(1)
API
(2)
APP範例
(1)
Array
(1)
AsyncTask
(1)
Auto Test Case
(32)
AutoCompleteTextView
(1)
Bitmap Drawable
(3)
BroadcastReceiver
(4)
Button
(1)
Codility
(2)
Contact
(4)
DB
(1)
Dialog
(2)
Documents
(1)
Eclipse
(3)
Ellipsize
(1)
File
(4)
Focus
(2)
Fragment
(4)
Gallery
(2)
GIT
(4)
GitHub
(1)
GridView
(8)
HashMap
(1)
HorizontalScrollView
(6)
IIS
(1)
Intent
(3)
IntentService
(1)
Internet
(2)
KeyEvent
(1)
Layout
(1)
ListView
(11)
Log
(1)
Mac / iOS
(11)
Manifest
(1)
Marquee
(2)
Math
(1)
MediaPlayer
(5)
MediaRecorder
(5)
MSMQ
(1)
onClick
(1)
PackageManager
(6)
PHP
(1)
PIS
(3)
PowerManager
(1)
Progress
(2)
SCREEN
(1)
Search
(6)
Service
(1)
SharedPreferences
(3)
SimpleDateFormat
(1)
SonarQube
(1)
Sound Recorder
(1)
Spinner
(2)
SQL server Management
(16)
SQLite
(13)
String
(1)
STS
(5)
SVN
(1)
Thread
(1)
Toast
(3)
Typeface
(1)
Uri
(2)
VB.NET
(17)
VMware
(1)
沒有留言:
張貼留言