android plugin download

FileDownLoadPlugin.java :

package org.apache.cordova;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

public class FileDownLoadPlugin extends Plugin {
	
	private static final String ACTION_PIC = "pic";
	private static final String STORE_PATH="wellapp/download/";

	public PluginResult execute(String action, JSONArray data, String callBackId) {
//		FileDownLoadPlugin plugin = this;
		PluginResult result = null;
		try {
			if (ACTION_PIC.equalsIgnoreCase(action)) {
				String urlStr = data.getString(1)+"?picId="+data.getString(0);
				Log.d("FileDownLoadPlugin execute", ACTION_PIC);
				// this.showDatePicker(callBackId);
				this.getImage(urlStr,data.getString(0)+".jpg");
				
				JSONObject userChoice = new JSONObject();
				userChoice.put("", "");
				result = new PluginResult(PluginResult.Status.OK,userChoice);
				result.setKeepCallback(true);
//				plugin.success(result, callBackId);
				return result;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public byte[] getImage(String urlStr,String fileName) throws Exception {
		Environment.getExternalStorageDirectory();
		URL url = new URL(urlStr);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 基于HTTP协议的连接对象
		conn.setConnectTimeout(5000);// 请求超时时间 5s
		conn.setRequestMethod("GET");// 请求方式
		Log.i("ResponseCode", "prepare");
		Log.i("ResponseCode", conn.getResponseCode() + "");
		if (conn.getResponseCode() == 200) {// 响应码==200 请求成功
			InputStream inputStream = conn.getInputStream();// 得到输入流
			Log.i("ResponseCode", inputStream.toString().length() + "");
			ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			Log.i("ResponseCode",
					"inputStream.read(buffer) :" + inputStream.read(buffer)
							+ "");
			while ((len = inputStream.read(buffer)) != -1) {
				arrayOutputStream.write(buffer, 0, len);
				Log.i("ResponseCode", "buffer.length :" + buffer.length + "  "
						+ len);
			}
			inputStream.close();
			
			Log.i("ResponseCode", "arrayOutputStream.toByteArray().length :"
					+ arrayOutputStream.toByteArray().length + "");
			
			File extDir = Environment.getExternalStorageDirectory();
			File fullFilename =new File(extDir,fileName);
			if(!fullFilename.exists()){
				fullFilename.mkdirs();
			}
			fullFilename.setWritable(Boolean.TRUE);
//			File imageFile = new File("BeautyGirl.txt"); 
			
			FileOutputStream outStream = new FileOutputStream(fullFilename);
			BufferedOutputStream bos = new BufferedOutputStream(outStream);
			bos.write(arrayOutputStream.toByteArray());  //此步骤保存的jpg未能打开,提示已损坏...
			
			//写入数据  
//			 YuvImage yuvimage=new YuvImage(arrayOutputStream.toByteArray(), ImageFormat.NV21, 20,20, null);
//			 yuvimage.compressToJpeg(new Rect(0, 0,20, 20), 80, arrayOutputStream);
//	        outStream.write(arrayOutputStream.toByteArray());  
	        
			//TODO 此处获取bitmap为空,未知原因,如何解决???
//	        Bitmap bitmap = BitmapFactory.decodeByteArray(arrayOutputStream.toByteArray(), 0,arrayOutputStream.toByteArray().length); 
//	        bitmap.compress(CompressFormat.JPEG, 100, bos);
	        
	        //关闭输出流  
	        arrayOutputStream.close();
	        outStream.close();  
		}
		return null;
	}
	

}




phonegap-filedownload.js

/**
 * 下载文件插件
 * 
 * @author cjianquan
 * @since 2014-07-18
 * 
 */
var FileDownLoad = function() {
};
FileDownLoad.prototype.downLoadFile = function(action, successCallback,
		failureCallback,args) {
	return PhoneGap.exec(successCallback, // Success callback from the plugin
	failureCallback, // Error callback from the plugin
	'FileDownLoadPlugin', // Tell PhoneGap to run "FileDownLoadPlugin" Plugin
	action, // Tell plugin, which action we want to perform
	args); // Passing list of args to the plugin
};

/**
 * 
 */

PhoneGap.addConstructor(function() {
	// 如果不支持window.plugins,则创建并设置
	if (!window.plugins) {
		window.plugins = {};
	}
	window.plugins.FileDownLoadPlugin = new FileDownLoad();
	// 向phonegap中注入插件相关的js
	// Register the javascript plugin with PhoneGap
	PhoneGap.addPlugin('FileDownLoadPlugin', new FileDownLoad());
	// phonegap中注入后台插件相关的java类
	// Register the native class of plugin with PhoneGap
	PluginManager.addService("FileDownLoadPlugin",
			"org.apache.cordova.FileDownLoadPlugin");
});


Reproduced in: https: //my.oschina.net/u/2552902/blog/543840

Guess you like

Origin blog.csdn.net/weixin_33717117/article/details/92326601