Jquery mobile PhoneGap下载插件

Jquery mobile PhoneGap下载插件,jquery-mobile,cordova,phonegap-plugins,Jquery Mobile,Cordova,Phonegap Plugins,这是我的phonegap和jquery应用程序的下载插件。该插件正在工作,但我想更改固定URL(“http://farm9.staticflickr.com/8010/7293758472_823d2eef5c_z.jpg)到当前URL(我想从当前URL下载jpg)。thanx Downloader.java package com.phonegap.plugins.downloader; import java.io.File; import java.io.FileNotFoundExce

这是我的phonegap和jquery应用程序的下载插件。该插件正在工作,但我想更改固定URL(“http://farm9.staticflickr.com/8010/7293758472_823d2eef5c_z.jpg)到当前URL(我想从当前URL下载jpg)。thanx

Downloader.java

package com.phonegap.plugins.downloader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

public class Downloader extends Plugin {

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {

    if (!action.equals("downloadFile")) 
        return new PluginResult(PluginResult.Status.INVALID_ACTION);

    try {

        String fileUrl = args.getString(0);
        JSONObject params = args.getJSONObject(1);

        String fileName = params.has("fileName") ? 
                params.getString("fileName"):
                fileUrl.substring(fileUrl.lastIndexOf("/")+1);


        String dirName = params.has("dirName") ?
                params.getString("dirName"):
                "sdcard/PhotoBunch";

        Boolean overwrite = params.has("overwrite") ? params.getBoolean("overwrite") : false;

        return this.downloadUrl(fileUrl, dirName, fileName, overwrite, callbackId);

    } catch (JSONException e) {

        e.printStackTrace();
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());

    } catch (InterruptedException e) {
        e.printStackTrace();
        return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
    }

}

private PluginResult downloadUrl(String fileUrl, String dirName, String fileName, Boolean overwrite, String callbackId) throws InterruptedException, JSONException {

    try {

        Log.d("PhoneGapLog", "Downloading "+fileUrl + " into " + dirName + "/" + fileName);

        File dir = new File(dirName);
        if (!dir.exists()) {
            Log.d("PhoneGapLog", "directory " + dirName + " created");
            dir.mkdirs();
        }

        File file = new File(dirName, fileName);

        if (!overwrite && file.exists()) {
            Log.d("DownloaderPlugin", "File already exist");

            JSONObject obj = new JSONObject();
            obj.put("status", 1);
            obj.put("total", 0);
            obj.put("file", fileName);
            obj.put("progress", 100);

            return new PluginResult(PluginResult.Status.OK, obj);
        }

        URL url = new URL(fileUrl);
        HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
        ucon.setRequestMethod("GET");
        ucon.setDoOutput(true);
        ucon.connect();

        Log.d("PhoneGapLog", "Download start");

        InputStream is = ucon.getInputStream();
        byte[] buffer = new byte[1024];
        int readed = 0, 
            progress = 0,
            totalReaded = 0,
            fileSize = ucon.getContentLength();

        FileOutputStream fos = new FileOutputStream(file);

        while ((readed = is.read(buffer)) > 0) {

            fos.write(buffer, 0, readed);
            totalReaded += readed;

            int newProgress = (int) (totalReaded*100/fileSize);             
            if (newProgress != progress)
             progress = informProgress(fileSize, newProgress, fileName, callbackId);

        }

        fos.close();

        Log.d("PhoneGapLog", "Download finished");

        JSONObject obj = new JSONObject();
        obj.put("status", 1);
        obj.put("total", fileSize);
        obj.put("file", fileName);
        obj.put("progress", progress);

        return new PluginResult(PluginResult.Status.OK, obj);

    }
    catch (FileNotFoundException e) {
        Log.d("PhoneGapLog", "File Not Found: " + e);
        return new PluginResult(PluginResult.Status.ERROR, 404);
    }
    catch (IOException e) {
        Log.d("PhoneGapLog", "Error: " + e);
        return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
    }

}

private int informProgress(int fileSize, int progress, String fileName, String callbackId) throws InterruptedException, JSONException {

    JSONObject obj = new JSONObject();
    obj.put("status", 0);
    obj.put("total", fileSize);
    obj.put("file", fileName);
    obj.put("progress", progress);

    PluginResult res = new PluginResult(PluginResult.Status.OK, obj);
    res.setKeepCallback(true);
    success(res, callbackId);

    //Give a chance for the progress to be sent to javascript
    Thread.sleep(100);

    return progress; 
} }
downloader.js

function Downloader() {}

Downloader.prototype.downloadFile = function(fileUrl, params, win, fail) {

//Make params hash optional.
if (!fail) win = params;
PhoneGap.exec(win, fail, "Downloader", "downloadFile", [fileUrl, params]);
};

PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("downloader", new Downloader());
PluginManager.addService("Downloader",        "com.phonegap.plugins.downloader.Downloader");
});
onClick=“download()”


您希望执行以下操作:

onclick="download(this.src)"

function download(currentUrl) {   
window.plugins.downloader.downloadFile(currentUrl, {overwrite: true}, 

  function(res) {
    alert(JSON.stringify(result));
}, function(error) {
    alert(error);
}
); }

currentURL:它是您从服务器下载的文件的文件名/完整路径

例如:

onclick="download(myfile.zip)"

function download(fileName) {   
window.plugins.downloader.downloadFile(currentUrl, {overwrite: true}, 

function(res) {
alert(JSON.stringify(result));}, 
function(error) {
alert(error);
}
); }

但它不起作用。it showng警报协议未找到:Null您为currentUrl传递的是什么?我正在使用Flickr api,这就是我无法放置url的原因(当我打开图片并单击“下载”时,我需要下载要下载的当前图像。设置中有大量拇指图片)什么是解决方案??…Thanx我将在currentUrl中传递什么?我认为如果你想显示要选择的图片的缩略图,然后下载,你需要做一些循环。。。
onclick="download(myfile.zip)"

function download(fileName) {   
window.plugins.downloader.downloadFile(currentUrl, {overwrite: true}, 

function(res) {
alert(JSON.stringify(result));}, 
function(error) {
alert(error);
}
); }