我试着将数据保存到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