From 980a1215731c20e840670d9ade77bc6bf64c255b Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 2 Mar 2020 22:38:53 +0800 Subject: Share image directly in ViewImageActivity. --- .../Activity/ViewGIFActivity.java | 2 +- .../Activity/ViewImageActivity.java | 54 +++++++++++++++++++-- .../Activity/ViewVideoActivity.java | 2 +- .../AsyncTask/SaveImageToFileAsyncTask.java | 55 ++++++++++++++++++++++ .../res/drawable/ic_share_toolbar_white_24dp.xml | 9 ++++ app/src/main/res/menu/view_gif_activity.xml | 11 ++++- app/src/main/res/menu/view_image.xml | 10 ---- app/src/main/res/menu/view_image_activity.xml | 17 +++++++ app/src/main/res/menu/view_video.xml | 4 +- app/src/main/res/values/strings.xml | 5 ++ app/src/main/res/xml/file_paths.xml | 3 +- 11 files changed, 151 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SaveImageToFileAsyncTask.java create mode 100644 app/src/main/res/drawable/ic_share_toolbar_white_24dp.xml delete mode 100644 app/src/main/res/menu/view_image.xml create mode 100644 app/src/main/res/menu/view_image_activity.xml (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java index 49b00060..90ae24c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java @@ -345,7 +345,7 @@ public class ViewGIFActivity extends AppCompatActivity { case android.R.id.home: finish(); return true; - case R.id.action_download_view_gif: + case R.id.action_download_view_gif_activity: if (isDownloading) { return false; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java index c90f0d60..933b3c59 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java @@ -9,6 +9,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; +import android.graphics.Bitmap; import android.graphics.PorterDuff; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; @@ -31,16 +32,20 @@ import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; +import androidx.core.content.FileProvider; import com.alexvasilkov.gestures.GestureController; import com.alexvasilkov.gestures.State; import com.alexvasilkov.gestures.views.GestureImageView; import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; +import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.target.Target; +import com.bumptech.glide.request.transition.Transition; import com.github.pwittchen.swipe.library.rx2.SimpleSwipeListener; import com.github.pwittchen.swipe.library.rx2.Swipe; @@ -50,6 +55,8 @@ import javax.inject.Inject; import butterknife.BindView; import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.AsyncTask.SaveImageToFileAsyncTask; +import ml.docilealligator.infinityforreddit.BuildConfig; import ml.docilealligator.infinityforreddit.ContentFontStyle; import ml.docilealligator.infinityforreddit.FontStyle; import ml.docilealligator.infinityforreddit.Infinity; @@ -82,6 +89,7 @@ public class ViewImageActivity extends AppCompatActivity { private float touchY = -1.0f; private float zoom = 1.0f; private boolean isSwiping = false; + private RequestManager glide; @Override protected void onCreate(Bundle savedInstanceState) { @@ -110,6 +118,8 @@ public class ViewImageActivity extends AppCompatActivity { actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor))); setTitle(""); + glide = Glide.with(this); + Intent intent = getIntent(); mImageUrl = intent.getStringExtra(IMAGE_URL_KEY); mImageFileName = intent.getStringExtra(FILE_NAME_KEY); @@ -333,7 +343,7 @@ public class ViewImageActivity extends AppCompatActivity { } private void loadImage() { - Glide.with(this).load(mImageUrl).listener(new RequestListener() { + glide.load(mImageUrl).listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { mProgressBar.setVisibility(View.GONE); @@ -352,7 +362,7 @@ public class ViewImageActivity extends AppCompatActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { mMenu = menu; - getMenuInflater().inflate(R.menu.view_image, menu); + getMenuInflater().inflate(R.menu.view_image_activity, menu); return true; } @@ -362,7 +372,7 @@ public class ViewImageActivity extends AppCompatActivity { case android.R.id.home: finish(); return true; - case R.id.action_download_view_image: + case R.id.action_download_view_image_activity: if (isDownloading) { return false; } @@ -387,6 +397,44 @@ public class ViewImageActivity extends AppCompatActivity { download(); } + return true; + case R.id.action_share_view_image_activity: + glide.asBitmap().load(mImageUrl).into(new CustomTarget() { + + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { + if (getExternalCacheDir() != null) { + new SaveImageToFileAsyncTask(resource, getExternalCacheDir().getPath(), + new SaveImageToFileAsyncTask.SaveImageToFileAsyncTaskListener() { + @Override + public void saveSuccess(File imageFile) { + Uri uri = FileProvider.getUriForFile(ViewImageActivity.this, + BuildConfig.APPLICATION_ID + ".provider",imageFile); + Intent shareIntent = new Intent(); + shareIntent.setAction(Intent.ACTION_SEND); + shareIntent.putExtra(Intent.EXTRA_STREAM, uri); + shareIntent.setType("image/*"); + shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + startActivity(Intent.createChooser(shareIntent, getString(R.string.share))); + } + + @Override + public void saveFailed() { + Toast.makeText(ViewImageActivity.this, + R.string.cannot_save_image, Toast.LENGTH_SHORT).show(); + } + }).execute(); + } else { + Toast.makeText(ViewImageActivity.this, + R.string.cannot_get_storage, Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); return true; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java index 6a2a4fdc..3ed60d5c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java @@ -385,7 +385,7 @@ public class ViewVideoActivity extends AppCompatActivity { case android.R.id.home: finish(); return true; - case R.id.action_download_view_video: + case R.id.action_download_view_video_activity: isDownloading = true; if (Build.VERSION.SDK_INT >= 23) { if (ContextCompat.checkSelfPermission(this, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SaveImageToFileAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SaveImageToFileAsyncTask.java new file mode 100644 index 00000000..07164a46 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SaveImageToFileAsyncTask.java @@ -0,0 +1,55 @@ +package ml.docilealligator.infinityforreddit.AsyncTask; + +import android.graphics.Bitmap; +import android.os.AsyncTask; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class SaveImageToFileAsyncTask extends AsyncTask { + private Bitmap resource; + private String cacheDirPath; + private SaveImageToFileAsyncTaskListener saveImageToFileAsyncTaskListener; + private boolean saveSuccess = true; + private File imageFile; + + public SaveImageToFileAsyncTask(Bitmap resource, String cacheDirPath, + SaveImageToFileAsyncTaskListener saveImageToFileAsyncTaskListener) { + this.resource = resource; + this.cacheDirPath = cacheDirPath; + this.saveImageToFileAsyncTaskListener = saveImageToFileAsyncTaskListener; + } + + public interface SaveImageToFileAsyncTaskListener { + void saveSuccess(File imageFile); + void saveFailed(); + } + + @Override + protected Void doInBackground(Void... voids) { + try { + imageFile = new File(cacheDirPath, "shared.jpg"); + OutputStream outputStream = new FileOutputStream(imageFile); + + resource.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); + + outputStream.flush(); + outputStream.close(); + } catch (IOException e) { + saveSuccess = false; + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (saveSuccess) { + saveImageToFileAsyncTaskListener.saveSuccess(imageFile); + } else { + saveImageToFileAsyncTaskListener.saveFailed(); + } + } +} diff --git a/app/src/main/res/drawable/ic_share_toolbar_white_24dp.xml b/app/src/main/res/drawable/ic_share_toolbar_white_24dp.xml new file mode 100644 index 00000000..cbe3d4aa --- /dev/null +++ b/app/src/main/res/drawable/ic_share_toolbar_white_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/menu/view_gif_activity.xml b/app/src/main/res/menu/view_gif_activity.xml index f97218ce..0dc5aba5 100644 --- a/app/src/main/res/menu/view_gif_activity.xml +++ b/app/src/main/res/menu/view_gif_activity.xml @@ -2,9 +2,16 @@ + app:showAsAction="ifRoom" /> + + \ No newline at end of file diff --git a/app/src/main/res/menu/view_image.xml b/app/src/main/res/menu/view_image.xml deleted file mode 100644 index c09ff1f8..00000000 --- a/app/src/main/res/menu/view_image.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - \ No newline at end of file diff --git a/app/src/main/res/menu/view_image_activity.xml b/app/src/main/res/menu/view_image_activity.xml new file mode 100644 index 00000000..1f86092b --- /dev/null +++ b/app/src/main/res/menu/view_image_activity.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/view_video.xml b/app/src/main/res/menu/view_video.xml index 71a52c58..fa54098f 100644 --- a/app/src/main/res/menu/view_video.xml +++ b/app/src/main/res/menu/view_video.xml @@ -2,9 +2,9 @@ + app:showAsAction="ifRoom" /> \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ccb251e4..cc7e8795 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -48,6 +48,7 @@ View Sidebar Save Delete Multireddit + Share Error occurred when parsing the JSON response Error Retrieving the token @@ -451,4 +452,8 @@ Enable NSFW Disable NSFW + + Cannot save the image + Cannot save the gif + Cannot access the app storage diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml index 79f4cf3f..25648473 100644 --- a/app/src/main/res/xml/file_paths.xml +++ b/app/src/main/res/xml/file_paths.xml @@ -1,5 +1,4 @@ - + \ No newline at end of file -- cgit v1.2.3