I am working on phonegap with android. i want to download a file from given url to my sd card.
**this is my 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>
<script type="text/javascript" charset="utf-8" src="downloader.js"></script>
<script type="text/javascript">
function down1()
{
window.plugins.downloader.downloadFile("http://192.168.1.214/sample/Winter.jpg","/mnt/sdcard/","archive.zip", false,
function(data){
if(data=="exist")
{
alert("File already exist");
}
else
{
alert("File saved on sd card")
}
},function(data){ alert("error is : "+data); });
}
</script>
</head>
<body>
<div data-role="button" onclick="down1();">Get data</div>
</body>
</html>
**this is my 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() {
console.log('=============i am in addConstructor================');
PhoneGap.addPlugin("downloader", new Downloader());
PluginManager.addService("Downloader","com.example.pgplugins.DownloaderPlugin");
});
**this is my Downloader.java**
package com.example.pgplugins.DownloaderPlugin;
//package com.example.pgplugins.downloaderPlugin;
/*
@author Mauro Rocco http://www.toforge.com
*/
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) {
System.out.println("=============i am in head class================");
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);
}
}
**and this is my simple main class:-**
package com.example.pgplugins.DownloaderPlugin;
import com.phonegap.*;
import android.os.Bundle;
public class musicdownloader extends DroidGap {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}
when i run this program, it is not doing anything. This is my whole project code, so please tell me what is mistake i have done.
4
A
回答
2
你是否按照此页面中的说明:https://github.com/phonegap/phonegap-plugins/tree/master/Android/Downloader我已经使用这个插件没有问题。
此外,我注意到URL是一个内部IP,电话或模拟器是否可以访问它?
要安装插件,请将downloader.js移动到您项目的www文件夹中,并在html文件中包含对其的引用。 在项目的src /文件夹中创建一个名为'com/phonegap/plugins/downloader'的文件夹。 然后将java文件复制到该新文件夹中。 以下内容添加到RES/XML/plugins.xml文件
1
有一个下载方法从URL中的PhoneGap 1.3.0
相关问题
- 1. 如何从网站下载文件到SD卡
- 2. 下载文件到sd卡不工作
- 3. 将音频文件从原始文件夹下载到SD卡
- 4. Android下载Zip到SD卡?
- 5. 科尔多瓦:从网址下载到Android下载文件夹
- 6. 从gwt网址下载文件
- 7. 下载文件从短网址
- 8. 从网址下载zip文件
- 9. 从cdn网址下载图片文件
- 10. 从网址下载批文件
- 11. Android - 从网址下载JSON文件
- 12. 如何从网址下载xml文件
- 13. ANDROID:如何将视频文件下载到SD卡?
- 14. 下载PDF文件并将其保存到SD卡
- 15. 下载文件并将其保存到SD卡
- 16. Android暂停并恢复将文件下载到SD卡
- 17. 下载APK到SD卡编程
- 18. 从.APK文件到SD卡的压缩
- 19. 保存文件到SD卡
- 20. 如何选择一个文件夹下载文件在SD卡
- 21. 如何将文件从网站保存到SD卡
- 22. 从Android SD卡上传文件到网络服务器
- 23. 图片加载SD卡从
- 24. 使用node.js将文件从网址下载到服务器
- 25. 如何通过ssh从网址下载文件到服务器
- 26. 如何将图像从网址下载到绘图文件夹?
- 27. 从互联网下载文件到我的下载文件夹
- 28. 使用PhoneGap从URL下载文件到文件系统
- 29. PhoneGap - 从计算机下载文件
- 30. 不能将图像从SD卡加载到网格视图
什么是对科尔多瓦3.4解决?现在这个链接已经死了。 –
尝试https://github.com/fastrde/phonegap-downloader –