对不起,
我没有Android文件系统的经验,我很难通过文档和教程来理解它。
我正在尝试将文件从某个位置复制到我的应用的外部存储空间。
final File filetobecopied =item.getFile();
File path=getPrivateExternalStorageDir(mContext);
final File destination = new File(path,item.getName());
try
{copy(filetobecopied,destination);
}
catch (IOException e) {Log.e("",e.toString());}
public void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Toast.makeText(mContext,"COPIED",Toast.LENGTH_SHORT).show();
}
public File getPrivateExternalStorageDir(Context context) {
File file = context.getExternalFilesDir(null);
if (!file.mkdirs()) {
Log.e("", "Directory not created");
}
return file;
}
我收到以下错误:
09-18 10:14:04.260: E/(7089): java.io.FileNotFoundException: /storage/emulated/0/Android/data/org.openintents.filemanager/files/2013-08-24 13.18.14.jpg: open failed: EISDIR (Is a directory)
答案 0 :(得分:2)
http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)使用此代码示例并使用google的标准示例
答案 1 :(得分:1)
尝试
final File destination = new File(path + "/" + item.getName());
代替。
答案 2 :(得分:1)
我认为,未创建文件夹目录或未安装外部存储状态。
在执行文件操作之前,您应该清理路径。以下代码是我经常使用的示例代码。
public File sanitizePath(Context context) {
String state = android.os.Environment.getExternalStorageState();
File folder = null;
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
folder = new File(context.getFilesDir() + path);
// path is the desired location that must be specified in your code.
}else{
folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path);
}
if (!folder.exists()){
folder.mkdirs();
}
return folder;
}
请确保在执行文件操作之前必须创建目录时。
希望这会有所帮助。
编辑:顺便说一下,您必须向manifest.xml文件添加以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />