mirror of https://github.com/M66B/FairEmail.git
Data URI improvements
This commit is contained in:
parent
4049621bc0
commit
4683f43b7c
|
@ -40,7 +40,6 @@ import android.content.SharedPreferences;
|
|||
import android.content.pm.PackageManager;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
|
@ -149,6 +148,7 @@ import org.jsoup.nodes.Document;
|
|||
import org.jsoup.nodes.Element;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
|
@ -4626,13 +4626,16 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
long id = args.getLong("id");
|
||||
String source = args.getString("source");
|
||||
|
||||
Bitmap bm = ImageHelper.getDataBitmap(source);
|
||||
if (bm == null)
|
||||
return null;
|
||||
String type = ImageHelper.getDataUriType(source);
|
||||
args.putString("type", type == null ? "application/octet-stream" : type);
|
||||
|
||||
File file = ImageHelper.getCacheFile(context, id, source, ".png");
|
||||
String extention = Helper.guessExtension(type);
|
||||
extention = "." + (extention == null ? "" : extention);
|
||||
|
||||
ByteArrayInputStream bis = ImageHelper.getDataUriStream(source);
|
||||
File file = ImageHelper.getCacheFile(context, id, source, extention);
|
||||
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
||||
bm.compress(Bitmap.CompressFormat.PNG, 90, os);
|
||||
Helper.copy(bis, os);
|
||||
}
|
||||
|
||||
return file;
|
||||
|
@ -4640,8 +4643,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
|||
|
||||
@Override
|
||||
protected void onExecuted(Bundle args, File file) {
|
||||
if (file != null)
|
||||
Helper.share(context, file, "image/png", file.getName());
|
||||
if (file == null)
|
||||
return;
|
||||
String type = args.getString("type");
|
||||
Helper.share(context, file, type, file.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -574,7 +574,7 @@ public class Helper {
|
|||
static void _share(Context context, File file, String type, String name) {
|
||||
// https://developer.android.com/reference/android/support/v4/content/FileProvider
|
||||
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);
|
||||
Log.i("uri=" + uri);
|
||||
Log.i("uri=" + uri + " type=" + type);
|
||||
|
||||
// Build intent
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
|
|
|
@ -59,6 +59,7 @@ import androidx.preference.PreferenceManager;
|
|||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
|
@ -313,7 +314,9 @@ class ImageHelper {
|
|||
// Data URI
|
||||
if (data && (show || inline || a.tracking))
|
||||
try {
|
||||
Bitmap bm = getDataBitmap(a.source);
|
||||
int scaleToPixels = res.getDisplayMetrics().widthPixels;
|
||||
ByteArrayInputStream bis = getDataUriStream(source);
|
||||
Bitmap bm = getScaledBitmap(bis, source, scaleToPixels);
|
||||
if (bm == null)
|
||||
throw new IllegalArgumentException("decode byte array failed");
|
||||
|
||||
|
@ -527,12 +530,24 @@ class ImageHelper {
|
|||
}
|
||||
}
|
||||
|
||||
static Bitmap getDataBitmap(String source) {
|
||||
static String getDataUriType(String source) {
|
||||
int colon = source.indexOf(':');
|
||||
if (colon < 0)
|
||||
return null;
|
||||
int semi = source.indexOf(';');
|
||||
if (semi < 0)
|
||||
return null;
|
||||
|
||||
return source.substring(colon + 1, semi);
|
||||
}
|
||||
|
||||
static ByteArrayInputStream getDataUriStream(String source) {
|
||||
// "<img src=\"data:image/png;base64,iVBORw0KGgoAAA" +
|
||||
// "ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
|
||||
// "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" +
|
||||
// "5ErkJggg==\" alt=\"Red dot\" />";
|
||||
|
||||
// https://en.wikipedia.org/wiki/Data_URI_scheme
|
||||
int comma = source.indexOf(',');
|
||||
if (comma < 0)
|
||||
return null;
|
||||
|
@ -540,7 +555,7 @@ class ImageHelper {
|
|||
String base64 = source.substring(comma + 1);
|
||||
byte[] bytes = Base64.decode(base64.getBytes(), 0);
|
||||
|
||||
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
||||
return new ByteArrayInputStream(bytes);
|
||||
}
|
||||
|
||||
private static Drawable getCachedImage(Context context, long id, String source) {
|
||||
|
|
Loading…
Reference in New Issue