我有一个我修改过的 phonegap 插件。Java 部分输出一个 base64 字符串:
package org.apache.cordova;
import java.io.ByteArrayOutputStream;
import java.io.File;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Base64;
import android.view.View;
public class Screenshot extends Plugin {
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
// starting on ICS, some WebView methods
// can only be called on UI threads
final Plugin that = this;
final String id = callbackId;
super.cordova.getActivity().runOnUiThread(new Runnable() {
//@Override
@TargetApi(8)
public void run() {
View view = webView.getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
if (!folder.exists()) {
folder.mkdirs();
}
File f = new File(folder, "screenshot_" + System.currentTimeMillis() + ".png");
System.out.println(folder);
System.out.println("screenshot_" + System.currentTimeMillis() + ".png");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String base64String = Base64.encodeToString(b, Base64.DEFAULT);
String mytextstring = "data:image/png;base64,"+base64String;
System.out.println(mytextstring);
that.success(new PluginResult(PluginResult.Status.OK, mytextstring), id);
}
});
PluginResult imageData = new PluginResult(PluginResult.Status.NO_RESULT);
imageData.setKeepCallback(true);
System.out.println("imageData=============>>>>>"+imageData);
return imageData;
}
}
然后我将它传递给一些 Javascript,然后将字符串发送到服务器。我检查了 .php 文件接收到的字符串,base64 字符串是相同的。但是,当我解码 base64 字符串时,它似乎已损坏。举个更好的例子,将此文本文件的内容复制到解码器中。
http://dl.dropbox.com/u/91982671/base64.txt
注意:当 .php 文件尝试对其进行解码时 data:image/png;base64, 在前面,我刚刚将其删除,以便您将其粘贴到解码器中。
解码器在这里找到:
http://www.motobit.com/util/base64-decoder-encoder.asp
我能想到的是,由于某种原因,我可能无法从 Java 中正确输出 base64 字符串。有谁知道发生了什么?或者是什么原因造成的?