在SD卡上创建文件失败,求解!!!

openmind_bluesky 发布于 2012/11/15 09:54
阅读 3K+
收藏 0

写了一个下载文件并写入 SD卡的程序,不知道为什么会创建文件失败,捕捉到的异常为红色部分,Logcat打印的info信息为:

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 失败


error信息为:

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)

根据info信息看,file文件创建成功了啊,不过我用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.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


import javax.net.ssl.HttpsURLConnection;


import android.util.Log;


public class HttpDownload {
private URL url = null;

//下载普通纯文本文件 
public String download(String urlStr){
String line = null;
StringBuffer sb = new StringBuffer();
BufferedReader bufr = null;

try {
//创建一个URL对象
url = new URL(urlStr);
//创建一个http连接
HttpURLConnection con =  (HttpURLConnection)url.openConnection();
//读取文件
bufr = new BufferedReader(new InputStreamReader(con.getInputStream()));
while((line = bufr.readLine())!=null){
sb.append(line);
}

} catch (Exception e) {
System.out.println("文本文件下载失败!");
}finally{

if(bufr!=null)
try {
bufr.close();
} catch (IOException e) {
System.out.println("文本文件读取流关闭失败!");
}
}
return sb.toString();

}

//可下载各种文件
//返回 -1:代表下载文件出错, 返回0:代表下载文件成功,返回 1:代表文件已经存在
//fileName代表你将要存入SD卡中的文件名,可以定义自己的文件名
public int downloadFile(String url,String path,String fileName){
InputStream in = null;
try {
FileUtils utils = new FileUtils() ;

if(utils.isFileExist(path+fileName)){
return 1;
}else{
in = getInputStreamFromUrl(url);
File resultFile = utils.write2SDFromInputStream(path, fileName, in);
if(resultFile == null){
return -1;
}
}

} catch (IOException e) {
e.printStackTrace();
return -1;
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
System.out.println("字节读取流关闭失败!");
 }
}
}

return 0;
}

//将根据Url获取InputStream的功能封装起来,以便复用
public InputStream getInputStreamFromUrl(String urlStr) throws IOException{

url = new URL(urlStr);
HttpURLConnection con =  (HttpURLConnection) url.openConnection();
InputStream in = con.getInputStream();
return in;
}
}


操作文件的工具类,用于在SD卡上创建文件夹、文件


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()+"/";
}

//在SD卡上创建目录
public File createSDDir(String dirName){
File dir = new File(SDPATH+dirName);
dir.mkdirs();
return dir;
}

//在SD卡上创建文件:
public File createSDFile(String dirAndFilename) throws IOException{
File file = new File(SDPATH+dirAndFilename);
file.createNewFile();
return file;


//判断SD卡上的文件夹是否存在
public boolean isFileExist(String fileName){
File file = new File(SDPATH + fileName);
return file.exists();
}


//将一个InputStream里面的数据写到SD卡中
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 失败");
}

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写入失败!");
e.printStackTrace();
}finally{

if(out!=null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("SD写入流关闭失败!");
}
}

return file;
}


}


加载中
0
鉴客
鉴客
目测空指针,创建文件成功写入数据失败,检查你要写入的数据是不是 null
0
平凡简单
平凡简单

com.example.Utils.HttpDownload.downloadFile(HttpDownload.java:70) 这里报null了

0
张仲尧
张仲尧
SDPATH = Environment.getExternalStorageDirectory().getPath()+"/";
改成这个试下
0
openmind_bluesky
openmind_bluesky

SDPATH = Environment.getExternalStorageDirectory().getPath()+"/";

试了,还是不行啊,郁闷至极,不过还是谢谢你啊

返回顶部
顶部