提交 02d095fd authored 作者: JarvanMo's avatar JarvanMo

fix:share large image on Android

上级 f9e5cc11
......@@ -15,6 +15,7 @@
*/
package com.jarvan.fluwx.handler
import android.util.Log
import com.jarvan.fluwx.constant.CallResult
import com.jarvan.fluwx.constant.WeChatPluginMethods
import com.jarvan.fluwx.constant.WechatPluginKeys
......@@ -25,6 +26,7 @@ import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry
import kotlinx.coroutines.*
import java.io.ByteArrayInputStream
/***
......@@ -170,11 +172,31 @@ internal class FluwxShareHandler {
val byteArray: ByteArray? = if (imagePath.isNullOrBlank()) {
byteArrayOf()
} else {
getImageByteArrayCommon(registrar, imagePath!!)
getImageByteArrayCommon(registrar, imagePath)
}
val imgObj = if (byteArray != null && byteArray.isNotEmpty()) {
if (byteArray.size > 512 * 1024){
val input = ByteArrayInputStream(byteArray)
val suffix = when {
imagePath.isNullOrBlank() -> ".jpeg"
imagePath.lastIndexOf(".") == -1 -> ".jpeg"
else -> imagePath.substring(imagePath.lastIndexOf("."))
}
val file = ShareImageUtil.inputStreamToFile(input,suffix,registrar!!.context())
WXImageObject().apply {
setImagePath(file.absolutePath)
}
}else{
WXImageObject(byteArray)
}
} else {
null
}
......
......@@ -15,19 +15,24 @@
*/
package com.jarvan.fluwx.utils;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import com.jarvan.fluwx.constant.WeChatPluginImageSchema;
import com.jarvan.fluwx.constant.WechatPluginKeys;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
......@@ -50,7 +55,7 @@ public class ShareImageUtil {
public static byte[] getImageData(PluginRegistry.Registrar registrar, String path) {
byte[] result = null;
if (path.startsWith(WeChatPluginImageSchema.SCHEMA_ASSETS)) {
String key = path.substring(WeChatPluginImageSchema.SCHEMA_ASSETS.length(), path.length());
String key = path.substring(WeChatPluginImageSchema.SCHEMA_ASSETS.length());
AssetFileDescriptor fileDescriptor = AssetManagerUtil.openAsset(registrar, key, getPackage(key));
try {
InputStream inputStream = fileDescriptor.createInputStream();
......@@ -118,14 +123,23 @@ public class ShareImageUtil {
return packageStr;
}
private static File inputStreamToTmpFile(InputStream inputStream, String suffix) {
public static File inputStreamToFile(InputStream inputStream, String suffix, Context context) {
File file = null;
BufferedSink sink = null;
Source source = null;
OutputStream outputStream = null;
try {
file = File.createTempFile(UUID.randomUUID().toString(), suffix);
File externalFile = context.getExternalCacheDir();
if (externalFile == null) {
return null;
}
file = new File(externalFile.getAbsolutePath()+File.separator+UUID.randomUUID().toString()+suffix);
// file = File.createTempFile(UUID.randomUUID().toString(), suffix);
outputStream = new FileOutputStream(file);
sink = Okio.buffer(Okio.sink(outputStream));
source = Okio.source(inputStream);
......@@ -162,6 +176,9 @@ public class ShareImageUtil {
return file;
}
private static InputStream openStream(String url) {
if(!url.startsWith("https") && !url.startsWith("http")){
url = "http://"+url;
......
......@@ -224,6 +224,11 @@ public class WeChatThumbnailUtil {
}
private static File downloadImage(String url) {
if(!url.startsWith("https") && !url.startsWith("http")){
url = "http://"+url;
}
File result = null;
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
Request request = new Request.Builder().url(url).get().build();
......
......@@ -9,8 +9,9 @@ class ShareImagePage extends StatefulWidget {
class _ShareImagePageState extends State<ShareImagePage> {
fluwx.WeChatScene scene = fluwx.WeChatScene.SESSION;
String _imagePath =
// "http://img-download.pchome.net/download/1k1/3a/3e/ofskcd-s1a.jpg"
"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534614311230&di=b17a892b366b5d002f52abcce7c4eea0&imgtype=0&src=http%3A%2F%2Fimg.mp.sohu.com%2Fupload%2F20170516%2F51296b2673704ae2992d0a28c244274c_th.png";
String _thumbnail = "assets://logo.png";
String _thumbnail = "assets://images/logo.png";
String _response = "";
......@@ -54,7 +55,7 @@ class _ShareImagePageState extends State<ShareImagePage> {
),
TextField(
decoration: InputDecoration(labelText: "缩略地址"),
controller: TextEditingController(text: "assets://logo.png"),
controller: TextEditingController(text: "assets://images/logo.png"),
onChanged: (value) {
_thumbnail = value;
},
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论