我試着將數據保存到SD卡的Android目錄問題,起初我試圖使用getExternalFilesDir app目錄內私下saave它externalStorage,但給我的NullPointerException所以我嘗試給其他方式下面,它的工作,但是當我想存儲文件到自定義目錄,我想自己命名它給我的錯誤:將圖片保存到SD卡
FileOutputStream os;
dirName = "/mydirectory/";
try {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)){
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + dirName);
dir.mkdirs();
//File file = new File(this.getExternalFilesDir(null), this.dirName+fileName); //this function give null pointer exception so im using other one
File file = new File(dir, dirName+fileName);
os = new FileOutputStream(file);
}else{
os = context.openFileOutput(fileName, MODE_PRIVATE);
}
resizedBitmap.compress(CompressFormat.PNG, 100, os);
os.flush();
os.close();
}catch(Exception e){
}
錯誤日誌:
java.io.FileNotFoundException:/mnt/sdcard/mvc/mvc/myfile2.png (沒有這樣的文件或目錄)
嘗試登錄要傳遞到所有的文件和目錄調用的參數 - 可能你沒有做什麼,你以爲你是。還要檢查所有的返回值,以便知道是否沒有工作。 – 2011-03-04 16:58:14
你有完整的堆棧跟蹤嗎? – thoredge 2011-03-04 17:10:26