Skip to content

Commit

Permalink
upload image
Browse files Browse the repository at this point in the history
  • Loading branch information
chinalwb committed Dec 17, 2018
1 parent 07fc166 commit 705ddfe
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -93,6 +99,7 @@ private void initToolbar() {

mEditText = this.findViewById(R.id.arEditText);
mEditText.setToolbar(mToolbar);
mEditText.setImageStrategy(imageStrategy);

setHtml();

Expand Down
Original file line number Diff line number Diff line change
@@ -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<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);
}
}
}
}
7 changes: 7 additions & 0 deletions ARE/are/src/main/java/com/chinalwb/are/AREditText.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
}
5 changes: 5 additions & 0 deletions ARE/are/src/main/java/com/chinalwb/are/AREditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -328,4 +329,8 @@ public void setVideoStrategy(VideoStrategy videoStrategy) {
this.mAre.setVideoStrategy(videoStrategy);
}

public void setImageStrategy(ImageStrategy imageStrategy) {
this.mAre.setImageStrategy(imageStrategy);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 705ddfe

Please sign in to comment.