From ad5e4df6e8f2cd747e55d55782a005db7337bbe1 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 3 Mar 2020 17:34:04 +0800 Subject: Share gif directly in ViewGifActivity. --- .../Activity/ViewGIFActivity.java | 50 ++++++++++++++++++- .../Activity/ViewImageActivity.java | 1 + .../AsyncTask/SaveGIFToFileAsyncTask.java | 57 ++++++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SaveGIFToFileAsyncTask.java 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 90ae24c1..5c641cff 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewGIFActivity.java @@ -31,10 +31,13 @@ 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.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.load.resource.gif.GifDrawable; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; @@ -47,12 +50,14 @@ import javax.inject.Inject; import butterknife.BindView; import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.AsyncTask.SaveGIFToFileAsyncTask; +import ml.docilealligator.infinityforreddit.BuildConfig; import ml.docilealligator.infinityforreddit.ContentFontStyle; import ml.docilealligator.infinityforreddit.FontStyle; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.TitleFontStyle; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import pl.droidsonroids.gif.GifImageView; public class ViewGIFActivity extends AppCompatActivity { @@ -74,6 +79,7 @@ public class ViewGIFActivity extends AppCompatActivity { private boolean isDownloading = false; private Menu mMenu; private Swipe swipe; + private RequestManager glide; private String mImageUrl; private String mImageFileName; private float totalLengthY = 0.0f; @@ -107,6 +113,8 @@ public class ViewGIFActivity 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); @@ -316,7 +324,7 @@ public class ViewGIFActivity 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); @@ -370,6 +378,44 @@ public class ViewGIFActivity extends AppCompatActivity { download(); } + return true; + case R.id.action_share_view_gif_activity: + Toast.makeText(ViewGIFActivity.this, R.string.save_gif_before_sharing, Toast.LENGTH_SHORT).show(); + glide.asGif().load(mImageUrl).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + return false; + } + + @Override + public boolean onResourceReady(GifDrawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + if (getExternalCacheDir() != null) { + new SaveGIFToFileAsyncTask(resource, getExternalCacheDir().getPath(), + new SaveGIFToFileAsyncTask.SaveGIFToFileAsyncTaskListener() { + @Override + public void saveSuccess(File imageFile) { + Uri uri = FileProvider.getUriForFile(ViewGIFActivity.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() { + + } + }).execute(); + } else { + Toast.makeText(ViewGIFActivity.this, + R.string.cannot_get_storage, Toast.LENGTH_SHORT).show(); + } + return false; + } + }).submit(); return true; } 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 933b3c59..c58d0736 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java @@ -404,6 +404,7 @@ public class ViewImageActivity extends AppCompatActivity { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { if (getExternalCacheDir() != null) { + Toast.makeText(ViewImageActivity.this, R.string.save_image_before_sharing, Toast.LENGTH_SHORT).show(); new SaveImageToFileAsyncTask(resource, getExternalCacheDir().getPath(), new SaveImageToFileAsyncTask.SaveImageToFileAsyncTaskListener() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SaveGIFToFileAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SaveGIFToFileAsyncTask.java new file mode 100644 index 00000000..39452004 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/SaveGIFToFileAsyncTask.java @@ -0,0 +1,57 @@ +package ml.docilealligator.infinityforreddit.AsyncTask; + +import android.os.AsyncTask; + +import com.bumptech.glide.load.resource.gif.GifDrawable; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; + +public class SaveGIFToFileAsyncTask extends AsyncTask { + private GifDrawable resource; + private String cacheDirPath; + private SaveGIFToFileAsyncTaskListener saveImageToFileAsyncTaskListener; + private boolean saveSuccess = true; + private File imageFile; + + public SaveGIFToFileAsyncTask(GifDrawable resource, String cacheDirPath, + SaveGIFToFileAsyncTaskListener saveImageToFileAsyncTaskListener) { + this.resource = resource; + this.cacheDirPath = cacheDirPath; + this.saveImageToFileAsyncTaskListener = saveImageToFileAsyncTaskListener; + } + + public interface SaveGIFToFileAsyncTaskListener { + void saveSuccess(File imageFile); + void saveFailed(); + } + + @Override + protected Void doInBackground(Void... voids) { + try { + imageFile = new File(cacheDirPath, "shared.gif"); + ByteBuffer byteBuffer = resource.getBuffer(); + OutputStream outputStream = new FileOutputStream(imageFile); + byte[] bytes = new byte[byteBuffer.capacity()]; + ((ByteBuffer) byteBuffer.duplicate().clear()).get(bytes); + outputStream.write(bytes, 0, bytes.length); + 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/values/strings.xml b/app/src/main/res/values/strings.xml index cc7e8795..41b3dde0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -456,4 +456,6 @@ Cannot save the image Cannot save the gif Cannot access the app storage + Saving the image. Please wait. + Saving the gif. Please wait. -- cgit v1.2.3