Data URI improvements

pull/199/head
M66B 3 years ago
parent 4049621bc0
commit 4683f43b7c

@ -40,7 +40,6 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Rect; import android.graphics.Rect;
@ -149,6 +148,7 @@ import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -4626,13 +4626,16 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
long id = args.getLong("id"); long id = args.getLong("id");
String source = args.getString("source"); String source = args.getString("source");
Bitmap bm = ImageHelper.getDataBitmap(source); String type = ImageHelper.getDataUriType(source);
if (bm == null) args.putString("type", type == null ? "application/octet-stream" : type);
return null;
String extention = Helper.guessExtension(type);
extention = "." + (extention == null ? "" : extention);
File file = ImageHelper.getCacheFile(context, id, source, ".png"); ByteArrayInputStream bis = ImageHelper.getDataUriStream(source);
File file = ImageHelper.getCacheFile(context, id, source, extention);
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
bm.compress(Bitmap.CompressFormat.PNG, 90, os); Helper.copy(bis, os);
} }
return file; return file;
@ -4640,8 +4643,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
@Override @Override
protected void onExecuted(Bundle args, File file) { protected void onExecuted(Bundle args, File file) {
if (file != null) if (file == null)
Helper.share(context, file, "image/png", file.getName()); return;
String type = args.getString("type");
Helper.share(context, file, type, file.getName());
} }
@Override @Override

@ -574,7 +574,7 @@ public class Helper {
static void _share(Context context, File file, String type, String name) { static void _share(Context context, File file, String type, String name) {
// https://developer.android.com/reference/android/support/v4/content/FileProvider // https://developer.android.com/reference/android/support/v4/content/FileProvider
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file); Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);
Log.i("uri=" + uri); Log.i("uri=" + uri + " type=" + type);
// Build intent // Build intent
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);

@ -59,6 +59,7 @@ import androidx.preference.PreferenceManager;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -313,7 +314,9 @@ class ImageHelper {
// Data URI // Data URI
if (data && (show || inline || a.tracking)) if (data && (show || inline || a.tracking))
try { try {
Bitmap bm = getDataBitmap(a.source); int scaleToPixels = res.getDisplayMetrics().widthPixels;
ByteArrayInputStream bis = getDataUriStream(source);
Bitmap bm = getScaledBitmap(bis, source, scaleToPixels);
if (bm == null) if (bm == null)
throw new IllegalArgumentException("decode byte array failed"); 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" + // "<img src=\"data:image/png;base64,iVBORw0KGgoAAA" +
// "ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" + // "ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
// "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" + // "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" +
// "5ErkJggg==\" alt=\"Red dot\" />"; // "5ErkJggg==\" alt=\"Red dot\" />";
// https://en.wikipedia.org/wiki/Data_URI_scheme
int comma = source.indexOf(','); int comma = source.indexOf(',');
if (comma < 0) if (comma < 0)
return null; return null;
@ -540,7 +555,7 @@ class ImageHelper {
String base64 = source.substring(comma + 1); String base64 = source.substring(comma + 1);
byte[] bytes = Base64.decode(base64.getBytes(), 0); 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) { private static Drawable getCachedImage(Context context, long id, String source) {

Loading…
Cancel
Save