在SD卡中创建文件失败

时间:2012-11-15 07:52:13

标签: android sd-card

我将下载文件写入SD卡程序。但我不知道为什么创建文件失败。 Logcat打印如下信息:

11-15 01:22:14.369: I/System.out(385): /mnt/sdcard/
11-15 01:22:16.413: I/System.out(385): file------->/mnt/sdcard/test/1.mp3
11-15 01:22:16.419: I/System.out(385): createSDFile

它捕获了错误:

11-15 00:21:29.248: W/dalvikvm(352): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-15 00:21:29.259: E/AndroidRuntime(352): FATAL EXCEPTION: main
11-15 00:21:29.259: E/AndroidRuntime(352): java.lang.NullPointerException
11-15 00:21:29.259: E/AndroidRuntime(352): at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
11-15 00:21:29.259: E/AndroidRuntime(352): at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
11-15 00:21:29.259: E/AndroidRuntime(352): at com.example.Utils.FileUtils.write2SDFromInputStream(FileUtils.java:60)
11-15 00:21:29.259: E/AndroidRuntime(352): at com.example.Utils.HttpDownload.downloadFile(HttpDownload.java:70)

由于信息,文件已正确创建。我使用fileExplorer check / mnt / sdcard /,并且找不到错误。路径中的'/'有问题吗? 网络连接权限:

<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

在sdcard中创建或删除文件的权限

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

呼叫下载方法:hdl.downloadFile("http://169.254.57.236/test/1.mp3","test/","1.mp3");

文件下载类

package com.example.Utils;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


import android.os.Environment;
import android.util.Log;


public class FileUtils {
private String SDPATH;


public FileUtils(){
SDPATH = Environment.getExternalStorageDirectory()+"/";
}

//create directory on SD card
public File createSDDir(String dirName){
File dir = new File(SDPATH+dirName);
dir.mkdirs();
return dir;
}

//cteate file on SD card:
public File createSDFile(String dirAndFilename) throws IOException{
File file = new File(SDPATH+dirAndFilename);
file.createNewFile();
return file;
} 

//check the file in SD card is exited or not
public boolean isFileExist(String fileName){
File file = new File(SDPATH + fileName);
return file.exists();
}


//put InputStream data into SD card
public File write2SDFromInputStream(String path,String fileName,InputStream in){
File file = null;
OutputStream out = null;

try {
createSDDir(path);
try{
file = createSDFile(path+fileName);
}

catch(Exception e){
System.out.println("createSDFile failed");
}

out = new FileOutputStream(file); 
byte buf[] = new byte[1024*5];
int ch = 0;
while((ch = in.read(buf))!=-1){
out.write(buf);
out.flush();
}

} catch (IOException e) {

System.out.println("SD write failed!");
e.printStackTrace();
}finally{

if(out!=null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("SD Write inflow closed failure!");
}
}

return file;
}


}

3 个答案:

答案 0 :(得分:2)

检查是否添加了清单权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:1)

尝试使用mkdirs()

            File sdCardDirectory = Environment
                    .getExternalStorageDirectory();
            new File(sdCardDirectory + "/XXX/Wallpapers/").mkdirs();
            File image = new File(sdCardDirectory
                    + "/XXX/Wallpapers/Sample" + numberOfImages + ".JPG");

并使用您的清单中的权限

          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

答案 2 :(得分:0)

没有代码,这几乎是不可能的。但你完成了创建文件吗?

File f;
f=new File(Environment.getExternalStorageDirectory() + "/whatever_dic");
f.createFile();