Answer the question
In order to leave comments, you need to log in
Android. Loading image into WebView from stream or byte array?
The task is this. There is a custom WebView, html content is loaded into it from a binary file of the internal format. There may also be images. According to the terms of the ToR, temporary files cannot be used.
I solved the problem by encoding the binary content of the image in base64 and placing it directly in the img tag. It works, but on large images it shows very low performance (an overhead for encoding in base64 and a significantly increased time for decoding and rendering for WebView). I see a workaround in overriding the image upload method and replacing it with my own implementation, slipping ready-made binary content, but searching Google and developer.android.com did not give anything.
Any ideas?
Answer the question
In order to leave comments, you need to log in
You can use image upload via content provider. The link from the page is obtained content://com.path.to.package/file.ext
In the manifest <provider android:name=".ImageProvider" android:authorities="com.path.to.package" />
In the provider class, define a function
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
File file = new File(....);
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
return parcel;
}
Something like this.
As it turns out, getFileDescriptor() returns null. With what it can be connected? I didn't find anything in the documentation about this.
I registered the content provider in the manifest, openFile is called, everything is as it should be.
In openFile itself, I do something like this:
byte[] imageData = ...;
Parcel parcel = Parcel.obtain();
parcel.writeByteArray(imageData, 0, imageData.length);
return parcel.getFileDescriptor();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question