提交 adbb16a7 authored 作者: Emily Fortuna's avatar Emily Fortuna

Correctly handle file paths and limit image upload size passed to WeChat.

上级 52df56db
...@@ -25,6 +25,8 @@ import okio.Source; ...@@ -25,6 +25,8 @@ import okio.Source;
public class ShareImageUtil { public class ShareImageUtil {
final static int WX_MAX_IMAGE_BYTE_SIZE = 10485760;
public static byte[] getImageData(PluginRegistry.Registrar registrar, String path) { public static byte[] getImageData(PluginRegistry.Registrar registrar, String path) {
byte[] result = null; byte[] result = null;
if (path.startsWith(WeChatPluginImageSchema.SCHEMA_ASSETS)) { if (path.startsWith(WeChatPluginImageSchema.SCHEMA_ASSETS)) {
...@@ -39,8 +41,14 @@ public class ShareImageUtil { ...@@ -39,8 +41,14 @@ public class ShareImageUtil {
} else if (path.startsWith(WeChatPluginImageSchema.SCHEMA_FILE)) { } else if (path.startsWith(WeChatPluginImageSchema.SCHEMA_FILE)) {
Bitmap bmp = null; Bitmap bmp = null;
bmp = BitmapFactory.decodeFile(path); String pathWithoutUri = path.substring("file://".length());
result = Util.bmpToByteArray(bmp, true); 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 { } else {
// result = handleNetworkImage(registrar, path); // result = handleNetworkImage(registrar, path);
result = Util.inputStreamToByte(openStream(path)); result = Util.inputStreamToByte(openStream(path));
......
...@@ -27,6 +27,24 @@ class Util { ...@@ -27,6 +27,24 @@ class Util {
return bmpToByteArray(bmp, CompressFormat.PNG, needRecycle); 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) { public static byte[] bmpToByteArray(final Bitmap bmp, CompressFormat format, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(format, 100, output); bmp.compress(format, 100, output);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论