Unverified 提交 1337d633 authored 作者: JarvanMo's avatar JarvanMo 提交者: GitHub

Merge pull request #8 from efortuna/master

Correctly handle file paths and limit image upload size passed to WX
......@@ -25,6 +25,8 @@ import okio.Source;
public class ShareImageUtil {
final static int WX_MAX_IMAGE_BYTE_SIZE = 10485760;
public static byte[] getImageData(PluginRegistry.Registrar registrar, String path) {
byte[] result = null;
if (path.startsWith(WeChatPluginImageSchema.SCHEMA_ASSETS)) {
......@@ -39,8 +41,14 @@ public class ShareImageUtil {
} else if (path.startsWith(WeChatPluginImageSchema.SCHEMA_FILE)) {
Bitmap bmp = null;
bmp = BitmapFactory.decodeFile(path);
result = Util.bmpToByteArray(bmp, true);
String pathWithoutUri = path.substring("file://".length());
bmp = BitmapFactory.decodeFile(pathWithoutUri);
if (bmp.getAllocationByteCount() >= WX_MAX_IMAGE_BYTE_SIZE) {
result = Util.bmpToCompressedByteArray(bmp, Bitmap.CompressFormat.JPEG, true);
} else {
result = Util.bmpToByteArray(bmp, true);
}
} else {
// result = handleNetworkImage(registrar, path);
result = Util.inputStreamToByte(openStream(path));
......
......@@ -27,6 +27,24 @@ class Util {
return bmpToByteArray(bmp, CompressFormat.PNG, needRecycle);
}
public static byte[] bmpToCompressedByteArray(final Bitmap bmp, CompressFormat format, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(format, 25, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static byte[] bmpToByteArray(final Bitmap bmp, CompressFormat format, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(format, 100, output);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论