Share image directly in ViewImageActivity.

This commit is contained in:
Alex Ning 2020-03-02 22:38:53 +08:00
parent 095dd30f71
commit 980a121573
11 changed files with 151 additions and 21 deletions

View File

@ -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;
}

View File

@ -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<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);
@ -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<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> 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;
}

View File

@ -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,

View File

@ -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<Void, Void, Void> {
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();
}
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92s2.92,-1.31 2.92,-2.92c0,-1.61 -1.31,-2.92 -2.92,-2.92zM18,4c0.55,0 1,0.45 1,1s-0.45,1 -1,1 -1,-0.45 -1,-1 0.45,-1 1,-1zM6,13c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1 1,0.45 1,1 -0.45,1 -1,1zM18,20.02c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1 1,0.45 1,1 -0.45,1 -1,1z"/>
</vector>

View File

@ -2,9 +2,16 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_download_view_gif"
android:id="@+id/action_download_view_gif_activity"
android:orderInCategory="1"
android:title="@string/action_download"
android:icon="@drawable/ic_file_download_toolbar_white_24dp"
app:showAsAction="always" />
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_share_view_gif_activity"
android:orderInCategory="2"
android:title="@string/action_share"
android:icon="@drawable/ic_share_toolbar_white_24dp"
app:showAsAction="ifRoom" />
</menu>

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_download_view_image"
android:orderInCategory="1"
android:title="@string/action_download"
android:icon="@drawable/ic_file_download_toolbar_white_24dp"
app:showAsAction="always" />
</menu>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_download_view_image_activity"
android:orderInCategory="1"
android:title="@string/action_download"
android:icon="@drawable/ic_file_download_toolbar_white_24dp"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_share_view_image_activity"
android:orderInCategory="2"
android:title="@string/action_share"
android:icon="@drawable/ic_share_toolbar_white_24dp"
app:showAsAction="ifRoom" />
</menu>

View File

@ -2,9 +2,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_download_view_video"
android:id="@+id/action_download_view_video_activity"
android:orderInCategory="1"
android:title="@string/action_download"
android:icon="@drawable/ic_file_download_toolbar_white_24dp"
app:showAsAction="always" />
app:showAsAction="ifRoom" />
</menu>

View File

@ -48,6 +48,7 @@
<string name="action_view_side_bar">View Sidebar</string>
<string name="action_save">Save</string>
<string name="action_delete_multi_reddit">Delete Multireddit</string>
<string name="action_share">Share</string>
<string name="parse_json_response_error">Error occurred when parsing the JSON response</string>
<string name="retrieve_token_error">Error Retrieving the token</string>
@ -451,4 +452,8 @@
<string name="enable_nsfw">Enable NSFW</string>
<string name="disable_nsfw">Disable NSFW</string>
<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>
</resources>

View File

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="image_temp"
path="Android/data/ml.docilealligator.infinityforreddit/files/Pictures" />
<external-path name="external_app_storage" path="." />
</paths>