在我的应用中,我获得了文件(doc,pdf,jpeg,...)的base64
数据,并使用FileWriter
编写。
该文件现在位于我的app目录中。但我需要将其移至Android设备的下载目录。
是否无法使用此代码访问:
window.resolveLocalFileSystemURL('content:///storage/emulated/0/Download', function(a) {},fail};
我总是收到错误代码1。
在我的Android manifest.xml
中,我目前只有WRITE_EXTERNAL_STORAGE
权限。这够了吗?
感谢。顺便说一句,我使用 cordova3.4 和 android 4.3 。
答案 0 :(得分:1)
sdcard路径在设备之间变化。
我建议您阅读File System plugin doc以获取完整信息。
如果您尚未修改config.xml以添加<preference name="AndroidPersistentFileLocation" value="Internal" />
,我会说这应该可以完成这项任务:
window.resolveLocalFileSystemURI('/Download', function(dirEntry) {},fail};
或者其他方式:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function (fs) {
fs.root.getDirectory("/Download", {
create : false,
exclusive : false
}, function (dirEntry) { //success
//do here what you need to do with Download folder
}, function () {
//error getDirectory
});
},
function () {
//error requestFileSystem
}
);
答案 1 :(得分:0)
这将帮助您在Cordova中将文件存储到您需要的目的地
公共类FileDownload扩展了CordovaPlugin {
int serverResponseCode = 0;
ProgressDialog dialog = null;
String upLoadServerUri = null;
final String DownloadFilePath = null;
final String DownloadFileNameOne = null;
private final String PATH = "/storage/sdcard0/DestinationFoloder/";
public static final String ACTION_FILE_UPLOAD = "fileUploadAction";
@Override
public boolean execute(String action, JSONArray args,CallbackContext callbackContext) throws JSONException {
try {
if (ACTION_FILE_UPLOAD.equals(action)) {
File dir = new File("/storage/sdcard0/Destination");
if (dir.mkdir()) {
Log.e("Directory created","DC");
} else {
Log.e("Directory Not created","DNC");
}
JSONObject arg_object = args.getJSONObject(0);
String DownloadFilePath = arg_object.getString("path");
String DownloadFileNameOne = arg_object.getString("NameDwn");
fdownload(DownloadFilePath,DownloadFileNameOne);
callbackContext.success("Attachment Download Sucessfully!");
}
return false;
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
callbackContext.error(e.getMessage());
return false;
}
}
private boolean fdownload(String downloadFilePath2, String downloadFileNameOne2) {
File FileCheck = new File(PATH+"/"+downloadFileNameOne2);
if(FileCheck.exists()){
return false;
}else{
try {
URL url = new URL(downloadFilePath2); // you can write here
// any
// link
Log.e("FilePath", "" + PATH + "" + downloadFileNameOne2);
File file = new File("" + PATH + "" + downloadFileNameOne2);
long startTime = System.currentTimeMillis();
URLConnection ucon;
ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to
* read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
}
答案 2 :(得分:0)
为什么不直接使用Cordova插件:cordova-plugin-file。具体的文件位置是:cordova.file.externalRootDirectory +&#34;下载/&#34;。所以将window.resolveLocalFileSystemURL()应用于此。