Android-Rich-text-Editor
Android-Rich-text-Editor copied to clipboard
Callback for image upload
I like this rte. One thing that is stopping me from fully trying this out is the image upload functionality. Such that, if I upload an image to the server I get an image url(server location), then I want to insert the image using this server image url. Do you have any way to achieve this functionality? Currently there doesn't seem to be any callback/method to achieve this.
I will check and back to you.
Thanks.
I added a new interface:
package com.chinalwb.are.strategies;
import android.net.Uri;
import com.chinalwb.are.styles.toolitems.styles.ARE_Style_Image;
public interface ImageStrategy {
/**
* Upload the video to server and return the url of the video at server.
* After that done, you need to call
* {@link ARE_Style_Image#insertImage(Object, com.chinalwb.are.spans.AreImageSpan.ImageType)}
* to insert the url on server to ARE
*
* @param uri
* @param areStyleImage used to insert the url on server to ARE
*/
void uploadAndInsertImage(Uri uri, ARE_Style_Image areStyleImage);
}
And this is a demo implementation:
package com.chinalwb.are.demo.helpers;
import android.app.ProgressDialog;
import android.net.Uri;
import android.os.AsyncTask;
import com.chinalwb.are.spans.AreImageSpan;
import com.chinalwb.are.strategies.ImageStrategy;
import com.chinalwb.are.styles.toolitems.styles.ARE_Style_Image;
import java.lang.ref.WeakReference;
import static android.os.AsyncTask.THREAD_POOL_EXECUTOR;
public class DemoImageStrategy implements ImageStrategy {
@Override
public void uploadAndInsertImage(Uri uri, ARE_Style_Image areStyleImage) {
new UploadImageTask(areStyleImage).executeOnExecutor(THREAD_POOL_EXECUTOR, uri);
}
private static class UploadImageTask extends AsyncTask<Uri, Integer, String> {
WeakReference<ARE_Style_Image> areStyleImage;
private ProgressDialog dialog;
UploadImageTask(ARE_Style_Image styleImage) {
this.areStyleImage = new WeakReference<>(styleImage);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if (dialog == null) {
dialog = ProgressDialog.show(
areStyleImage.get().getEditText().getContext(),
"",
"Uploading image. Please wait...",
true);
} else {
dialog.show();
}
}
@Override
protected String doInBackground(Uri... uris) {
if (uris != null && uris.length > 0) {
try {
// do upload here ~
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Returns the image url on server here
return "https://avatars0.githubusercontent.com/u/1758864?s=460&v=4";
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (dialog != null) {
dialog.dismiss();
}
if (areStyleImage.get() != null) {
areStyleImage.get().insertImage(s, AreImageSpan.ImageType.URL);
}
}
}
}
And set it to are:
com.chinalwb.are.AREditText#setImageStrategy
Sample usage here:
com.chinalwb.are.demo.ARE_DefaultToolbarActivity
And here is demo a video:

Hope this helps you.
Currently it is in repository master. I'll publish 0.1.5 to enable this feature.
Thanks.
Thanks for doing it. I'll check it out once you publish it. And will post an update here.
I did publish (oh I thought I had posted this comment but not...).
Try this:
implementation 'com.github.bumptech.glide:glide:4.3.1'
implementation 'com.github.chinalwb:are:0.1.5'
Are you sure you published changes with DemoImageStrategy? I'm seeing cannot resolve symbol 'DemoImageStrategy' error with 0.1.5 release. Also why the name is DemoImageStrategy? Can't it resemble to what its actually doing?
Also how do I use uploadAndInsertImage() in my activity?
DemoImageStrategy is not published within the library, it is in Github.
Checkout the latest code and run the demo. Thanks.