在android的sd卡中能创建文件但是不能创建文件夹怎么解决

a465456465 2012-06-01 03:46:33
各位高烧怎么在sd卡中能创建文件就是不能创建文件夹呢?权限我全加上了
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

public static void checkedAndSaveFile(String fileName,String data,String path){
InputStream inputStream = new java.io.ByteArrayInputStream(StringUtil.fromBase64(data));
String savePath = path.trim();
savePath = savePath.replace("\\", "/");
String saveFileName = fileName.trim();
String saveFilePathAndName = savePath+File.separator+saveFileName;
Log.i("dir======================>", saveFilePathAndName);
saveFilePathAndName=saveFilePathAndName.trim();
File newFile = new File(saveFilePathAndName);
File fileDir = new File(savePath);
if(!fileDir.exists()){
fileDir.mkdir();
}
//
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "test.txt");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
//
if(!newFile.exists())
try {
newFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if(checkSDCard()){
save(saveFilePathAndName, inputStream, newFile);
}
}

public static void save(String fileName,InputStream inputStream,File file){

FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
byte[] buffer = new byte[512];
int len;
int downloader = 0;
while ((len = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
downloader += len;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fos.flush();
fos.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
...全文
2870 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kd_jacky 2014-01-15
  • 打赏
  • 举报
回复
终于找到问题所在了 感谢楼主
wtrience 2013-12-21
  • 打赏
  • 举报
回复
引用 2 楼 RDroid 的回复:
如果目录的上一级目录不存在的话,mkdir()会失败 要确保创建目录成功,最好使用 mkdirs()来创建
恩人啊!
RDroid 2012-06-02
  • 打赏
  • 举报
回复
如果目录的上一级目录不存在的话,mkdir()会失败
要确保创建目录成功,最好使用 mkdirs()来创建
OnMyJavaWay 2012-06-01
  • 打赏
  • 举报
回复
if(!fileDir.exists()){
fileDir.mkdir();
}

fileDir.mkdirs();

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧