diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-03-03 09:34:04 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-03-03 09:34:04 +0000 |
commit | ad5e4df6e8f2cd747e55d55782a005db7337bbe1 (patch) | |
tree | bf5488f03022a13112e709e21f5c18a225a57b6b /app | |
parent | 980a1215731c20e840670d9ade77bc6bf64c255b (diff) | |
download | infinity-for-reddit-ad5e4df6e8f2cd747e55d55782a005db7337bbe1.tar infinity-for-reddit-ad5e4df6e8f2cd747e55d55782a005db7337bbe1.tar.gz infinity-for-reddit-ad5e4df6e8f2cd747e55d55782a005db7337bbe1.tar.bz2 infinity-for-reddit-ad5e4df6e8f2cd747e55d55782a005db7337bbe1.tar.lz infinity-for-reddit-ad5e4df6e8f2cd747e55d55782a005db7337bbe1.tar.xz infinity-for-reddit-ad5e4df6e8f2cd747e55d55782a005db7337bbe1.tar.zst infinity-for-reddit-ad5e4df6e8f2cd747e55d55782a005db7337bbe1.zip |
Share gif directly in ViewGifActivity.
Diffstat (limited to 'app')
4 files changed, 108 insertions, 2 deletions
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<Drawable>() { + glide.load(mImageUrl).listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { mProgressBar.setVisibility(View.GONE); @@ -371,6 +379,44 @@ public class ViewGIFActivity extends AppCompatActivity { } 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<GifDrawable>() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) { + return false; + } + + @Override + public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> 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; } 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 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<? super Bitmap> 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<Void, Void, Void> { + 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 @@ <string name="cannot_save_image">Cannot save the image</string> <string name="cannot_save_gif">Cannot save the gif</string> <string name="cannot_get_storage">Cannot access the app storage</string> + <string name="save_image_before_sharing">Saving the image. Please wait.</string> + <string name="save_gif_before_sharing">Saving the gif. Please wait.</string> </resources> |