Implement image uploading in PostGalleryActivity.

This commit is contained in:
Alex Ning 2021-07-16 19:53:55 +08:00
parent 25d8d901ad
commit 89704cd652
5 changed files with 72 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
@ -33,14 +34,19 @@ import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.switchmaterial.SwitchMaterial;
import com.google.gson.Gson;
import com.libRG.CustomTextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParserException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import javax.inject.Inject;
@ -53,6 +59,7 @@ import ml.docilealligator.infinityforreddit.Flair;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.RedditGalleryPayload;
import ml.docilealligator.infinityforreddit.adapters.RedditGallerySubmissionRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment;
@ -60,7 +67,9 @@ import ml.docilealligator.infinityforreddit.bottomsheetfragments.SelectOrCapture
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent;
import ml.docilealligator.infinityforreddit.services.SubmitPostService;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.utils.UploadImageUtils;
import pl.droidsonroids.gif.GifImageView;
import retrofit2.Retrofit;
@ -165,7 +174,8 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
private FlairBottomSheetFragment flairSelectionBottomSheetFragment;
private Snackbar mPostingSnackbar;
private RedditGallerySubmissionRecyclerViewAdapter adapter;
private Uri captureImageUri;
private Uri imageUri;
private boolean isUploading;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -201,8 +211,12 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
adapter = new RedditGallerySubmissionRecyclerViewAdapter(this, mCustomThemeWrapper, new RedditGallerySubmissionRecyclerViewAdapter.ItemClickListener() {
@Override
public void onAddImageClicked() {
SelectOrCaptureImageBottomSheetFragment fragment = new SelectOrCaptureImageBottomSheetFragment();
fragment.show(getSupportFragmentManager(), fragment.getTag());
if (!isUploading) {
SelectOrCaptureImageBottomSheetFragment fragment = new SelectOrCaptureImageBottomSheetFragment();
fragment.show(getSupportFragmentManager(), fragment.getTag());
} else {
Snackbar.make(coordinatorLayout, R.string.please_wait_image_is_uploading, Snackbar.LENGTH_SHORT).show();
}
}
});
imagesRecyclerView.setAdapter(adapter);
@ -387,9 +401,9 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
public void captureImage() {
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
captureImageUri = FileProvider.getUriForFile(this, "ml.docilealligator.infinityforreddit.provider",
imageUri = FileProvider.getUriForFile(this, "ml.docilealligator.infinityforreddit.provider",
File.createTempFile("temp_img", ".jpg", getExternalFilesDir(Environment.DIRECTORY_PICTURES)));
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, captureImageUri);
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(pictureIntent, CAPTURE_IMAGE_REQUEST_CODE);
} catch (IOException ex) {
Snackbar.make(coordinatorLayout, R.string.error_creating_temp_file, Snackbar.LENGTH_SHORT).show();
@ -398,6 +412,34 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
}
}
private void uploadImage() {
Handler handler = new Handler();
isUploading = true;
mExecutor.execute(() -> {
try {
Bitmap resource = Glide.with(PostGalleryActivity.this).asBitmap().load(imageUri).submit().get();
String response = UploadImageUtils.uploadImage(mOauthRetrofit, mUploadMediaRetrofit, mAccessToken, resource, true);
RedditGalleryPayload.Item item = new Gson().fromJson(new JSONObject(response).getJSONObject(JSONUtils.ASSET_KEY).toString(), RedditGalleryPayload.Item.class);
handler.post(() -> {
adapter.setImageAsUploaded(item);
isUploading = false;
});
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
handler.post(() -> {
Snackbar.make(coordinatorLayout, R.string.get_image_bitmap_failed, Snackbar.LENGTH_LONG).show();
isUploading = false;
});
} catch (XmlPullParserException | JSONException | IOException e) {
e.printStackTrace();
handler.post(() -> {
Snackbar.make(coordinatorLayout, R.string.error_processing_image, Snackbar.LENGTH_LONG).show();
isUploading = false;
});
}
});
}
private void displaySubredditIcon() {
if (iconUrl != null && !iconUrl.equals("")) {
mGlide.load(iconUrl)
@ -560,11 +602,14 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
return;
}
adapter.addImage(data.getData().toString());
imageUri = data.getData();
adapter.addImage(imageUri.toString());
uploadImage();
}
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
adapter.addImage(captureImageUri.toString());
adapter.addImage(imageUri.toString());
uploadImage();
}
}
}

View File

@ -5,6 +5,7 @@ import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -82,14 +83,13 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
((ImageViewHolder) holder).progressBar.setVisibility(View.GONE);
((ImageViewHolder) holder).closeImageView.setVisibility(View.VISIBLE);
return false;
}
})
.into(((ImageViewHolder) holder).imageView);
if (redditGalleryImageInfoList.get(position).payload != null) {
Log.i("asdfasdf", "asdf ");
((ImageViewHolder) holder).progressBar.setVisibility(View.GONE);
((ImageViewHolder) holder).closeImageView.setVisibility(View.VISIBLE);
}
@ -128,6 +128,11 @@ public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Ada
notifyItemInserted(redditGalleryImageInfoList.size() - 1);
}
public void setImageAsUploaded(RedditGalleryPayload.Item item) {
redditGalleryImageInfoList.get(redditGalleryImageInfoList.size() - 1).payload = item;
notifyItemChanged(redditGalleryImageInfoList.size() - 1);
}
class ImageViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.aspect_ratio_gif_image_view_item_reddit_gallery_submission_image)
AspectRatioGifImageView imageView;

View File

@ -164,4 +164,5 @@ public class JSONUtils {
public static final String ESTIMATED_REMAINING_TIME_KEY = "estimated_remaining_time";
public static final String PAYLOAD_KEY = "payload";
public static final String AUTHOR_ICON_IMAGE = "author_icon_img";
public static final String ASSET_KEY = "asset";
}

View File

@ -28,7 +28,13 @@ import retrofit2.Retrofit;
public class UploadImageUtils {
@Nullable
public static String uploadImage(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
String accessToken, Bitmap image) throws IOException, JSONException, XmlPullParserException {
String accessToken, Bitmap image) throws IOException, JSONException, XmlPullParserException {
return uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, image, false);
}
@Nullable
public static String uploadImage(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
String accessToken, Bitmap image, boolean returnResponseForGallerySubmission) throws IOException, JSONException, XmlPullParserException {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
Map<String, String> uploadImageParams = new HashMap<>();
@ -51,6 +57,9 @@ public class UploadImageUtils {
Call<String> uploadMediaToAWS = uploadMediaToAWSApi.uploadMediaToAWS(nameValuePairsMap, fileToUpload);
Response<String> uploadMediaToAWSResponse = uploadMediaToAWS.execute();
if (uploadMediaToAWSResponse.isSuccessful()) {
if (returnResponseForGallerySubmission) {
return uploadImageResponse.body();
}
return parseXMLResponseFromAWS(uploadMediaToAWSResponse.body());
} else {
return "Error: " + uploadMediaToAWSResponse.code();

View File

@ -1138,4 +1138,6 @@
<string name="search_comments">Search Comments</string>
<string name="please_wait_image_is_uploading">An image is still being uploaded. Please wait.</string>
</resources>