Load data: urls in markdown
This commit is contained in:
@@ -59,6 +59,7 @@ import com.github.gotify.messages.provider.MessageDeletion;
|
||||
import com.github.gotify.messages.provider.MessageFacade;
|
||||
import com.github.gotify.messages.provider.MessageState;
|
||||
import com.github.gotify.messages.provider.MessageWithImage;
|
||||
import com.github.gotify.picasso.PicassoDataRequestHandler;
|
||||
import com.github.gotify.service.WebSocketService;
|
||||
import com.google.android.material.navigation.NavigationView;
|
||||
import com.google.android.material.snackbar.BaseTransientBottomBar;
|
||||
@@ -247,7 +248,10 @@ public class MessagesActivity extends AppCompatActivity
|
||||
|
||||
OkHttp3Downloader downloader = new OkHttp3Downloader(builder.build());
|
||||
|
||||
return new Picasso.Builder(this).downloader(downloader).build();
|
||||
return new Picasso.Builder(this)
|
||||
.addRequestHandler(new PicassoDataRequestHandler())
|
||||
.downloader(downloader)
|
||||
.build();
|
||||
}
|
||||
|
||||
private void initDrawer() {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.github.gotify.picasso;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.util.Base64;
|
||||
import com.github.gotify.log.Log;
|
||||
import com.squareup.picasso.Picasso;
|
||||
import com.squareup.picasso.Request;
|
||||
import com.squareup.picasso.RequestHandler;
|
||||
|
||||
/**
|
||||
* Adapted from https://github.com/square/picasso/issues/1395#issuecomment-220929377 By
|
||||
* https://github.com/SmartDengg
|
||||
*/
|
||||
public class PicassoDataRequestHandler extends RequestHandler {
|
||||
|
||||
private static final String DATA_SCHEME = "data";
|
||||
|
||||
@Override
|
||||
public boolean canHandleRequest(Request data) {
|
||||
String scheme = data.uri.getScheme();
|
||||
return DATA_SCHEME.equalsIgnoreCase(scheme);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result load(Request request, int networkPolicy) {
|
||||
String uri = request.uri.toString();
|
||||
String imageDataBytes = uri.substring(uri.indexOf(",") + 1);
|
||||
byte[] bytes = Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT);
|
||||
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
||||
|
||||
if (bitmap == null) {
|
||||
String show = uri.length() > 50 ? uri.substring(0, 49) + "..." : uri;
|
||||
RuntimeException malformed = new RuntimeException("Malformed data uri: " + show);
|
||||
Log.e("Could not load image", malformed);
|
||||
throw malformed;
|
||||
}
|
||||
|
||||
return new Result(bitmap, Picasso.LoadedFrom.NETWORK);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user