Android 将文件从url下载到sdk卡中
我正在与android项目合作phonegap。我想从url下载一个文件到我的sdk卡中 这是我的Downloader.javaAndroid 将文件从url下载到sdk卡中,android,cordova,Android,Cordova,我正在与android项目合作phonegap。我想从url下载一个文件到我的sdk卡中 这是我的Downloader.java package com.example.pgplugins.downloaderPlugin; import org.json.JSONArray; import org.json.JSONException; import android.util.Log; import com.phonegap.DroidGap; import com.phonegap.ap
package com.example.pgplugins.downloaderPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
import com.phonegap.DroidGap;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Downloader extends Plugin{
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
if (action.equals("downloadFile")) {
try {
return this.downloadUrl(args.getString(0),args.getString(1),args.getString(2),args.getString(3));
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
}
}
else {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
}
private PluginResult downloadUrl(String fileUrl, String dirName, String fileName, String overwrite){
try{
Log.d("DownloaderPlugin", "DIRECTORY CALLED /sdcard/"+dirName+" created");
File dir = new File("/sdcard/"+dirName);
if(!dir.exists()){
Log.d("DownloaderPlugin", "directory /sdcard/"+dirName+" created");
dir.mkdirs();
}
File file = new File("/sdcard/"+dirName+fileName);
if(overwrite.equals("false") && file.exists()){
Log.d("DownloaderPlugin", "File already exist");
return new PluginResult(PluginResult.Status.OK, "exist");
}
URL url = new URL(fileUrl);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setRequestMethod("GET");
ucon.setDoOutput(true);
ucon.connect();
Log.d("DownloaderPlugin", "download begining");
Log.d("DownloaderPlugin", "download url:" + url);
InputStream is = ucon.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
FileOutputStream fos = new FileOutputStream(file);
while ( (len1 = is.read(buffer)) > 0 ) {
fos.write(buffer,0, len1);
}
fos.close();
Log.d("DownloaderPlugin", "Download complete in" + fileName);
} catch (IOException e) {
Log.d("DownloaderPlugin", "Error: " + e);
return new PluginResult(PluginResult.Status.ERROR, "Error: " + e);
}
return new PluginResult(PluginResult.Status.OK, fileName);
}
}
这是我的Downloader.js
function Downloader() {
}
Downloader.prototype.downloadFile = function(fileUrl,dirName,fileName,overwrite,win,fail) {
if(overwrite==false) overwrite="false";
else overwrite="true";
PhoneGap.exec(win, fail, "Downloader", "downloadFile", [fileUrl,dirName,fileName,overwrite]);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("downloader", new Downloader());
PluginManager.addService("Downloader","com.example.pgplugins.downloaderPlugin.Downloader");
});
这是我的index.html
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>PhoneGap Demo With JQuery Mobile</title>
<link rel="stylesheet" href="jquery.mobile/jquery.mobile-1.0b2.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="pgandjqm-style-override.css" type="text/css" charset="utf-8" />
<script type="text/javascript" src="jquery.mobile/jquery-1.6.2.min"></script>
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script src="jquery.mobile/jquery.mobile-1.0b2.js"></script>
<script type="text/javascript" charset="utf-8" src="main.js"></script>
<!-- CDN Respositories: For production, replace lines above with these uncommented minified versions -->
<!-- <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css" />-->
<!-- <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>-->
<!-- <script src="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.js"></script>-->
script type="text/javascript" charset="utf-8" src="downloader.js"></script>
<script type="text/javascript">
window.plugins.downloader.downloadFile("http://www.toforge.com/archive.zip","sdcard/cache/","archive.zip", false,
function(data){
if(data=="exist"){
alert("File already exist");
}
else{
alert("File saved on sd card")
}
},function(data){ alert("error: "+data); });
</script>
</head>
<body onload="init();">
</body>
</html>
使用JQuery Mobile的PhoneGap演示
script type=“text/javascript”charset=“utf-8”src=“downloader.js”>
window.plugins.downloader.downloadFile(“http://www.toforge.com/archive.zip“,“sdcard/cache/”,“archive.zip”,false,
功能(数据){
如果(数据==“存在”){
警报(“文件已存在”);
}
否则{
警报(“文件保存在sd卡上”)
}
},函数(数据){alert(“error:+data);});
每当我运行那个程序时,就会出现这个错误,这意味着我得不到任何结果
错误:-强制关闭程序
我的程序未运行。意味着在开始运行之前,会发生暂停。请检查我的代码并帮助我找出错误。如果要使用带有通知的服务在后台下载文件,请检查此项
我希望这会对您有所帮助。这是logcat中发生的错误:-07-26 16:48:55.328:DEBUG/AndroidRuntime(275):关闭VM 07-26 16:48:55.328:WARN/dalvikvm(275):threadid=1:线程退出时出现未捕获异常(组=0x4001d800)07-26 16:48:55.435:error/AndroidRuntime(275):致命异常:main 07-26 16:48:55.435:错误/AndroidRuntime(275):java.lang.RuntimeException:无法在执行它停止的任何函数之前实例化activitymeans。