在我的应用程序中,我获取文件的
base64
数据(doc、pdf、jpeg...)并用 FileWriter
写入。
该文件现在位于我的应用程序目录中。但我需要将其移动到Android设备的下载目录。
使用此代码是否无法访问:
window.resolveLocalFileSystemURL('content:///storage/emulated/0/Download', function(a) {},fail};
我总是收到错误代码 1。
在我的 Android
manifest.xml
中,我目前只有 WRITE_EXTERNAL_STORAGE
权限。这够了吗?
谢谢。顺便说一句,我使用cordova3.4和android 4.3。
SD卡路径因设备而异。
我建议您阅读文件系统插件文档以获取完整信息。
如果您还没有修改 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
}
);
这将帮助您在科尔多瓦将文件存储到您所需的目的地
公共类 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;
}
}
}
}
为什么不直接使用 Cordova 插件:cordova-plugin-file。 具体文件位置为:cordova.file.externalRootDirectory + “Download/”。 因此,请应用 window.resolveLocalFileSystemURL() 。
var destinationPath = cordova.file.externalRootDirectory + "下载/"; // 目标是下载文件夹 window.resolveLocalFileSystemURL(storageLocation, function (fileEntry) {
// Get the destination directory
window.resolveLocalFileSystemURL(destinationPath, function (dirEntry) {
// Move the file
fileEntry.moveTo(dirEntry, fileName, function (newFileEntry) {
console.log("File moved successfully to: " + newFileEntry.nativeURL);
}, function (error) {
console.error("Error moving file: " + error.code);
});
}, function (error) {
console.error("Error resolving destination directory: " + error.code);
});
}, function (error) {
console.error("Error resolving source file: " + error.code);
});