From 705ddfe0ad3f7828d134cc9d0c4636d95eda7e0a Mon Sep 17 00:00:00 2001 From: Rain Liu Date: Mon, 17 Dec 2018 20:48:06 +0800 Subject: [PATCH] upload image --- .../are/demo/ARE_DefaultToolbarActivity.java | 7 ++ .../are/demo/helpers/DemoImageStrategy.java | 75 +++++++++++++++++++ .../java/com/chinalwb/are/AREditText.java | 7 ++ .../main/java/com/chinalwb/are/AREditor.java | 5 ++ .../are/strategies/ImageStrategy.java | 19 +++++ .../styles/toolitems/ARE_ToolItem_Image.java | 9 +++ 6 files changed, 122 insertions(+) create mode 100644 ARE/app/src/main/java/com/chinalwb/are/demo/helpers/DemoImageStrategy.java create mode 100644 ARE/are/src/main/java/com/chinalwb/are/strategies/ImageStrategy.java diff --git a/ARE/app/src/main/java/com/chinalwb/are/demo/ARE_DefaultToolbarActivity.java b/ARE/app/src/main/java/com/chinalwb/are/demo/ARE_DefaultToolbarActivity.java index d747968..233e437 100644 --- a/ARE/app/src/main/java/com/chinalwb/are/demo/ARE_DefaultToolbarActivity.java +++ b/ARE/app/src/main/java/com/chinalwb/are/demo/ARE_DefaultToolbarActivity.java @@ -1,6 +1,7 @@ package com.chinalwb.are.demo; import android.content.Intent; +import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; @@ -10,6 +11,8 @@ import android.widget.ImageView; import com.chinalwb.are.AREditText; +import com.chinalwb.are.demo.helpers.DemoImageStrategy; +import com.chinalwb.are.strategies.ImageStrategy; import com.chinalwb.are.styles.toolbar.ARE_ToolbarDefault; import com.chinalwb.are.styles.toolbar.IARE_Toolbar; import com.chinalwb.are.styles.toolitems.ARE_ToolItem_AlignmentCenter; @@ -31,6 +34,7 @@ import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Underline; import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Video; import com.chinalwb.are.styles.toolitems.IARE_ToolItem; +import com.chinalwb.are.styles.toolitems.styles.ARE_Style_Image; import static com.chinalwb.are.demo.TextViewActivity.HTML_TEXT; @@ -42,6 +46,8 @@ public class ARE_DefaultToolbarActivity extends AppCompatActivity { private boolean scrollerAtEnd; + private ImageStrategy imageStrategy = new DemoImageStrategy(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -93,6 +99,7 @@ private void initToolbar() { mEditText = this.findViewById(R.id.arEditText); mEditText.setToolbar(mToolbar); + mEditText.setImageStrategy(imageStrategy); setHtml(); diff --git a/ARE/app/src/main/java/com/chinalwb/are/demo/helpers/DemoImageStrategy.java b/ARE/app/src/main/java/com/chinalwb/are/demo/helpers/DemoImageStrategy.java new file mode 100644 index 0000000..a3f662d --- /dev/null +++ b/ARE/app/src/main/java/com/chinalwb/are/demo/helpers/DemoImageStrategy.java @@ -0,0 +1,75 @@ +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 { + + WeakReference 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); + } + } + } +} diff --git a/ARE/are/src/main/java/com/chinalwb/are/AREditText.java b/ARE/are/src/main/java/com/chinalwb/are/AREditText.java index b9a5eb9..714762e 100644 --- a/ARE/are/src/main/java/com/chinalwb/are/AREditText.java +++ b/ARE/are/src/main/java/com/chinalwb/are/AREditText.java @@ -2,6 +2,7 @@ import android.content.Context; import android.graphics.Color; +import android.media.Image; import android.os.Build; import android.support.v7.widget.AppCompatEditText; import android.text.Editable; @@ -29,6 +30,7 @@ import com.chinalwb.are.spans.AreSuperscriptSpan; import com.chinalwb.are.spans.AreUnderlineSpan; import com.chinalwb.are.strategies.AtStrategy; +import com.chinalwb.are.strategies.ImageStrategy; import com.chinalwb.are.strategies.VideoStrategy; import com.chinalwb.are.styles.ARE_Helper; import com.chinalwb.are.styles.IARE_Style; @@ -385,4 +387,9 @@ public static void stopMonitor() { private VideoStrategy mVideoStrategy; public void setVideoStrategy(VideoStrategy videoStrategy) { mVideoStrategy = videoStrategy; } public VideoStrategy getVideoStrategy() { return mVideoStrategy; } + + // ImageStrategy + private ImageStrategy mImageStrategy; + public void setImageStrategy(ImageStrategy imageStrategy) { mImageStrategy = imageStrategy; } + public ImageStrategy getImageStrategy() { return mImageStrategy; } } diff --git a/ARE/are/src/main/java/com/chinalwb/are/AREditor.java b/ARE/are/src/main/java/com/chinalwb/are/AREditor.java index 4a5f490..d93fa0c 100644 --- a/ARE/are/src/main/java/com/chinalwb/are/AREditor.java +++ b/ARE/are/src/main/java/com/chinalwb/are/AREditor.java @@ -15,6 +15,7 @@ import com.chinalwb.are.render.AreImageGetter; import com.chinalwb.are.render.AreTagHandler; import com.chinalwb.are.strategies.AtStrategy; +import com.chinalwb.are.strategies.ImageStrategy; import com.chinalwb.are.strategies.VideoStrategy; import com.chinalwb.are.styles.toolbar.ARE_Toolbar; @@ -328,4 +329,8 @@ public void setVideoStrategy(VideoStrategy videoStrategy) { this.mAre.setVideoStrategy(videoStrategy); } + public void setImageStrategy(ImageStrategy imageStrategy) { + this.mAre.setImageStrategy(imageStrategy); + } + } diff --git a/ARE/are/src/main/java/com/chinalwb/are/strategies/ImageStrategy.java b/ARE/are/src/main/java/com/chinalwb/are/strategies/ImageStrategy.java new file mode 100644 index 0000000..3e34da9 --- /dev/null +++ b/ARE/are/src/main/java/com/chinalwb/are/strategies/ImageStrategy.java @@ -0,0 +1,19 @@ +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); +} diff --git a/ARE/are/src/main/java/com/chinalwb/are/styles/toolitems/ARE_ToolItem_Image.java b/ARE/are/src/main/java/com/chinalwb/are/styles/toolitems/ARE_ToolItem_Image.java index 1d891f2..f410508 100644 --- a/ARE/are/src/main/java/com/chinalwb/are/styles/toolitems/ARE_ToolItem_Image.java +++ b/ARE/are/src/main/java/com/chinalwb/are/styles/toolitems/ARE_ToolItem_Image.java @@ -7,6 +7,7 @@ import android.text.Editable; import android.text.style.CharacterStyle; import android.text.style.StyleSpan; +import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; @@ -18,11 +19,14 @@ import com.chinalwb.are.activities.Are_VideoPlayerActivity; import com.chinalwb.are.models.AtItem; import com.chinalwb.are.spans.AreImageSpan; +import com.chinalwb.are.strategies.ImageStrategy; import com.chinalwb.are.styles.ARE_At; import com.chinalwb.are.styles.IARE_Style; import com.chinalwb.are.styles.toolitems.styles.ARE_Style_Bold; import com.chinalwb.are.styles.toolitems.styles.ARE_Style_Image; +import java.io.File; + /** * Created by wliu on 13/08/2018. */ @@ -72,6 +76,11 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { if (ARE_Style_Image.REQUEST_CODE == requestCode) { ARE_Style_Image imageStyle = (ARE_Style_Image) getStyle(); Uri uri = data.getData(); + ImageStrategy imageStrategy = this.getEditText().getImageStrategy(); + if (imageStrategy != null) { + imageStrategy.uploadAndInsertImage(uri, imageStyle); + return; + } imageStyle.insertImage(uri, AreImageSpan.ImageType.URI); } }