Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display an image in the RichEditor with an Intent #289

Closed
Clement-Cauet opened this issue Feb 24, 2023 · 1 comment
Closed

Display an image in the RichEditor with an Intent #289

Clement-Cauet opened this issue Feb 24, 2023 · 1 comment

Comments

@Clement-Cauet
Copy link

Hello,
I can't choose an image in my local storage, display the image in my RichEditor and stock the image in my database (I stock my html in my database) in same time.
I work with android 11 API 30.
My AndroidManifest.xml containt this lines :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

If I use this line : instanceFragment.getRichEditor().insertImage("https://raw.githubusercontent.com/wasabeef/art/master/chip.jpg", "image", 300);
The image is display in the RichEditor and I can stock in my database but I have don't choose my image in my local storage.

If I use this line : instanceFragment.getRichEditor().insertImage("File:https://" + destination.getPath(), "image", 300);
The image isn't display in the RichEditor (the alt is display) but I can stock in my database and I have choose my image in my local storage.

If I use this line : instanceFragment.getRichEditor().insertImage(url, "image", 300);
The image is display in the RichEditor and I have choose my image in my local storage but I can't stock in my database (because this is a temp url).

This is my code :
RichEditor.txt

Can you help me please ?
Thank in advance.

@Clement-Cauet
Copy link
Author

Clement-Cauet commented Apr 25, 2023

Hello,
I found a solution to insert an image from the phone's internal storage to the RichEditor, you can encode the image in base64. This allows you to store the image in a database and to bypass the various security features of Android and HTML.
Here is the code to do this:

//- Create image file in local storage
File destination = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "image.jpg");

//- Encoding image in base64
byte[] imageBytes = new byte[0];
try {
      imageBytes = Files.toByteArray(new File(destination.getPath()));
      String imageBase64 = BaseEncoding.base64().encode(imageBytes);
      instanceFragment.getRichEditor().insertImage("data:image/jpg;base64," + imageBase64, destination.getName(), 100);
} catch (IOException e) {
      throw new RuntimeException(e);
}

Don't forget to format the file type, in the example I use a jpg format, but you could use another format like a png format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant