2012-06-10 5 views
0

내 phonegap 및 jquery 앱용 내 다운로더 플러그인입니다.이 플러그인은 작동하지만 그걸 변경하고 싶습니다. 고정 URL ("http://farm9.staticflickr.com/8010/7293758472_823d2eef5c_z.jpg ") 현재 URL에 .. (내가있는 현재의 URL에서 JPG를 다운로드하려면이) Downloader.javaPhoneGap 다운로드 플러그인

을 .thanx

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 = "다운로드()"

function download() { 
window.plugins.downloader.downloadFile("http://farm9.staticflickr.com/8010/7293758472_823d2eef5c_z.jpg", {overwrite: true}, 

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

답변

0

당신은이 같은 싶지 :

onclick="download(this.src)" 

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

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

thanx 그러나 작동하지 않습니다. 알았어 ** 프로토콜을 찾을 수 없음 : null ** – tempor

+0

currentUrl에 대해 무엇을 전달하고 있습니까? –

+0

Flickr api thats를 사용하고 있는데 왜 URL을 넣을 수 없습니까 (사진을 열 때 현재 이미지를 다운로드하려면 download.here에 엄지 그림이 많이 있음) 솔루션이 무엇입니까 ?? ... .thanx – tempor

1

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); 
} 
); } 
관련 문제