0

我将下载文件写入 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 检查 /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;
}


}
4

3 回答 3

2

检查是否添加清单权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-11-15T07:58:30.137 回答
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" /> 
于 2012-11-15T09:07:35.087 回答
0

没有代码,这几乎是不可能的。但是您是否已经创建了文件?

File f;
f=new File(Environment.getExternalStorageDirectory() + "/whatever_dic");
f.createFile(); 
于 2012-11-15T08:01:32.617 回答