我已经搜索了很多关于我的问题的信息,但我真的不知道我做错了什么,我只是想将图像保存到特定文件夹中的sdcard,但是folder.mkdir()总是返回false我得到一个例外
java.io.FileNotFoundException: /mnt/sdcard/sakhcomcache/tv/1.gif: open failed: ENOENT (No such file or directory)
当然我在我的清单中有权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
这是我的代码
.....
private final static String CACHE_PATH = "/sakhcomcache/";
public final static String CACHE_FOLDER_TV = "/tv";
.....
public static void saveImageOnSDCard(final Bitmap image, final String cacheFolder, final String name) {
new Thread(new Runnable() {
@Override
public void run() {
try {
File folder = new File(Environment.getExternalStorageDirectory() + CACHE_PATH + cacheFolder);
if (!folder.exists()) {
boolean create_succes = folder.mkdir();
//create_succes always false
if(create_succes){
Log.i("create_succes", "create_succes");
}
}
File imageFile = new File(folder +"/"+ name.substring(name.lastIndexOf("/")));
if (!imageFile.exists()) {
FileOutputStream out = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Log.i("save succes", "save succes");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
答案 0 :(得分:6)
试试这个
//创建目录
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "myDir" + File.separator);
root.mkdirs();
directoryCreated = new File(root, "filename");
//保存文件
FileOutputStream out = new FileOutputStream(directoryCreated);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
bm.recycle();