Maybe fix java.lang.RuntimeException: Canvas: trying to draw too large bitmap. Fix many bugs.

This commit is contained in:
Alex Ning 2020-06-15 22:05:33 +08:00
parent e8e85b9631
commit b57d381e94
11 changed files with 72 additions and 52 deletions

View File

@ -89,7 +89,6 @@ public class ViewImageActivity extends AppCompatActivity implements SetAsWallpap
private String mImageUrl; private String mImageUrl;
private String mImageFileName; private String mImageFileName;
private RequestManager glide; private RequestManager glide;
private String postTitle;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -131,7 +130,7 @@ public class ViewImageActivity extends AppCompatActivity implements SetAsWallpap
Intent intent = getIntent(); Intent intent = getIntent();
mImageUrl = intent.getStringExtra(IMAGE_URL_KEY); mImageUrl = intent.getStringExtra(IMAGE_URL_KEY);
mImageFileName = intent.getStringExtra(FILE_NAME_KEY); mImageFileName = intent.getStringExtra(FILE_NAME_KEY);
postTitle = intent.getStringExtra(POST_TITLE_KEY); String postTitle = intent.getStringExtra(POST_TITLE_KEY);
if (postTitle != null) { if (postTitle != null) {
setTitle(Html.fromHtml(String.format("<small>%s</small>", postTitle))); setTitle(Html.fromHtml(String.format("<small>%s</small>", postTitle)));
@ -185,7 +184,7 @@ public class ViewImageActivity extends AppCompatActivity implements SetAsWallpap
mProgressBar.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE);
return false; return false;
} }
}).into(mImageView); }).override(Target.SIZE_ORIGINAL).into(mImageView);
} }
@Override @Override

View File

@ -287,10 +287,12 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
} }
private void setToolbarTitle(int position) { private void setToolbarTitle(int position) {
if (images.get(position).getType() == ImgurMedia.TYPE_VIDEO) { if (images != null && position >= 0 && position < images.size()) {
setTitle(getString(R.string.view_imgur_media_activity_video_label, position + 1, images.size())); if (images.get(position).getType() == ImgurMedia.TYPE_VIDEO) {
} else { setTitle(getString(R.string.view_imgur_media_activity_video_label, position + 1, images.size()));
setTitle(getString(R.string.view_imgur_media_activity_image_label, position + 1, images.size())); } else {
setTitle(getString(R.string.view_imgur_media_activity_image_label, position + 1, images.size()));
}
} }
} }
@ -382,17 +384,23 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa
@Override @Override
public void setToHomeScreen(int viewPagerPosition) { public void setToHomeScreen(int viewPagerPosition) {
setAsWallpaper(images.get(viewPagerPosition).getLink(), 0); if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) {
setAsWallpaper(images.get(viewPagerPosition).getLink(), 0);
}
} }
@Override @Override
public void setToLockScreen(int viewPagerPosition) { public void setToLockScreen(int viewPagerPosition) {
setAsWallpaper(images.get(viewPagerPosition).getLink(), 1); if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) {
setAsWallpaper(images.get(viewPagerPosition).getLink(), 1);
}
} }
@Override @Override
public void setToBoth(int viewPagerPosition) { public void setToBoth(int viewPagerPosition) {
setAsWallpaper(images.get(viewPagerPosition).getLink(), 2); if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) {
setAsWallpaper(images.get(viewPagerPosition).getLink(), 2);
}
} }
public int getCurrentPagePosition() { public int getCurrentPagePosition() {

View File

@ -701,23 +701,25 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
fetchSubredditData(); fetchSubredditData();
return true; return true;
case R.id.action_lazy_mode_view_subreddit_detail_activity: case R.id.action_lazy_mode_view_subreddit_detail_activity:
MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_subreddit_detail_activity); if (sectionsPagerAdapter != null) {
if (isInLazyMode) { MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_subreddit_detail_activity);
isInLazyMode = false; if (isInLazyMode) {
sectionsPagerAdapter.stopLazyMode(); isInLazyMode = false;
lazyModeItem.setTitle(R.string.action_start_lazy_mode); sectionsPagerAdapter.stopLazyMode();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | lazyModeItem.setTitle(R.string.action_start_lazy_mode);
AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED); params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS |
collapsingToolbarLayout.setLayoutParams(params); AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED);
} else {
isInLazyMode = true;
if (sectionsPagerAdapter.startLazyMode()) {
lazyModeItem.setTitle(R.string.action_stop_lazy_mode);
appBarLayout.setExpanded(false);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
collapsingToolbarLayout.setLayoutParams(params); collapsingToolbarLayout.setLayoutParams(params);
} else { } else {
isInLazyMode = false; isInLazyMode = true;
if (sectionsPagerAdapter.startLazyMode()) {
lazyModeItem.setTitle(R.string.action_stop_lazy_mode);
appBarLayout.setExpanded(false);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
collapsingToolbarLayout.setLayoutParams(params);
} else {
isInLazyMode = false;
}
} }
} }
return true; return true;
@ -777,7 +779,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
@Override @Override
public void sortTypeSelected(SortType sortType) { public void sortTypeSelected(SortType sortType) {
sectionsPagerAdapter.changeSortType(sortType); if (sectionsPagerAdapter != null) {
sectionsPagerAdapter.changeSortType(sortType);
}
} }
@Override @Override

View File

@ -1117,7 +1117,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
private void loadImage(PostDetailBaseViewHolder holder) { private void loadImage(PostDetailBaseViewHolder holder) {
if (holder instanceof PostDetailImageAndGifAutoplayViewHolder) { if (holder instanceof PostDetailImageAndGifAutoplayViewHolder) {
String url = mAutoplay && mPost.getPostType() == Post.GIF_TYPE ? mPost.getUrl() : mPost.getPreviewUrl(); String url = mAutoplay && mPost.getPostType() == Post.GIF_TYPE ? mPost.getUrl() : mPost.getPreviewUrl();
RequestBuilder imageRequestBuilder = mGlide.load(url) RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(url)
.listener(new RequestListener<Drawable>() { .listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
@ -1140,12 +1140,12 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
if ((mPost.isNSFW() && mNeedBlurNsfw) || (mPost.isSpoiler() && mNeedBlurSpoiler)) { if ((mPost.isNSFW() && mNeedBlurNsfw) || (mPost.isSpoiler() && mNeedBlurSpoiler)) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); .override(Target.SIZE_ORIGINAL).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView);
} else { } else {
imageRequestBuilder.into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView);
} }
} else if (holder instanceof PostDetailVideoAndGifPreviewHolder) { } else if (holder instanceof PostDetailVideoAndGifPreviewHolder) {
RequestBuilder imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(mPost.getPreviewUrl())
.listener(new RequestListener<Drawable>() { .listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
@ -1168,12 +1168,12 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
if ((mPost.isNSFW() && mNeedBlurNsfw) || (mPost.isSpoiler() && mNeedBlurSpoiler)) { if ((mPost.isNSFW() && mNeedBlurNsfw) || (mPost.isSpoiler() && mNeedBlurSpoiler)) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); .override(Target.SIZE_ORIGINAL).into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView);
} else { } else {
imageRequestBuilder.into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView);
} }
} else if(holder instanceof PostDetailLinkViewHolder) { } else if(holder instanceof PostDetailLinkViewHolder) {
RequestBuilder imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(mPost.getPreviewUrl())
.listener(new RequestListener<Drawable>() { .listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
@ -1196,9 +1196,9 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
if ((mPost.isNSFW() && mNeedBlurNsfw) || (mPost.isSpoiler() && mNeedBlurSpoiler)) { if ((mPost.isNSFW() && mNeedBlurNsfw) || (mPost.isSpoiler() && mNeedBlurSpoiler)) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostDetailLinkViewHolder) holder).mImageView); .override(Target.SIZE_ORIGINAL).into(((PostDetailLinkViewHolder) holder).mImageView);
} else { } else {
imageRequestBuilder.into(((PostDetailLinkViewHolder) holder).mImageView); imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailLinkViewHolder) holder).mImageView);
} }
} }
} }

View File

@ -1145,9 +1145,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostImageAndGifAutoplayViewHolder) holder).imageView); .override(Target.SIZE_ORIGINAL).into(((PostImageAndGifAutoplayViewHolder) holder).imageView);
} else { } else {
imageRequestBuilder.into(((PostImageAndGifAutoplayViewHolder) holder).imageView); imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostImageAndGifAutoplayViewHolder) holder).imageView);
} }
} else if (holder instanceof PostVideoAndGifPreviewViewHolder) { } else if (holder instanceof PostVideoAndGifPreviewViewHolder) {
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener<Drawable>() { RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener<Drawable>() {
@ -1173,9 +1173,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostVideoAndGifPreviewViewHolder) holder).imageView); .override(Target.SIZE_ORIGINAL).into(((PostVideoAndGifPreviewViewHolder) holder).imageView);
} else { } else {
imageRequestBuilder.into(((PostVideoAndGifPreviewViewHolder) holder).imageView); imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostVideoAndGifPreviewViewHolder) holder).imageView);
} }
} else if (holder instanceof PostLinkTypeViewHolder) { } else if (holder instanceof PostLinkTypeViewHolder) {
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener<Drawable>() { RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener<Drawable>() {
@ -1201,9 +1201,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
.into(((PostLinkTypeViewHolder) holder).imageView); .override(Target.SIZE_ORIGINAL).into(((PostLinkTypeViewHolder) holder).imageView);
} else { } else {
imageRequestBuilder.into(((PostLinkTypeViewHolder) holder).imageView); imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostLinkTypeViewHolder) holder).imageView);
} }
} else if (holder instanceof PostCompactViewHolder) { } else if (holder instanceof PostCompactViewHolder) {
String previewUrl = post.getThumbnailPreviewUrl().equals("") ? post.getPreviewUrl() : post.getThumbnailPreviewUrl(); String previewUrl = post.getThumbnailPreviewUrl().equals("") ? post.getPreviewUrl() : post.getThumbnailPreviewUrl();
@ -1224,9 +1224,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) {
imageRequestBuilder imageRequestBuilder
.transform(new BlurTransformation(50, 2)) .transform(new BlurTransformation(50, 2))
.into(((PostCompactViewHolder) holder).imageView); .override(Target.SIZE_ORIGINAL).into(((PostCompactViewHolder) holder).imageView);
} else { } else {
imageRequestBuilder.into(((PostCompactViewHolder) holder).imageView); imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostCompactViewHolder) holder).imageView);
} }
} }
} }

View File

@ -279,7 +279,7 @@ public class CommentData implements Parcelable {
if (children == null || children.size() == 0) { if (children == null || children.size() == 0) {
setChildren(moreChildren); setChildren(moreChildren);
} else { } else {
if (children.get(children.size() - 1).isPlaceHolder) { if (children.size() > 1 && children.get(children.size() - 1).isPlaceHolder) {
children.addAll(children.size() - 2, moreChildren); children.addAll(children.size() - 2, moreChildren);
} else { } else {
children.addAll(moreChildren); children.addAll(moreChildren);

View File

@ -100,7 +100,7 @@ public class ViewImgurImageFragment extends Fragment {
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
return false; return false;
} }
}).into(imageView); }).override(Target.SIZE_ORIGINAL).into(imageView);
} }
@Override @Override

View File

@ -397,7 +397,7 @@ public class DownloadRedditVideoService extends Service {
int audioTrack = muxer.addTrack(audioFormat); int audioTrack = muxer.addTrack(audioFormat);
boolean sawEOS = false; boolean sawEOS = false;
int offset = 100; int offset = 100;
int sampleSize = 256 * 1024; int sampleSize = 2048 * 1024;
ByteBuffer videoBuf = ByteBuffer.allocate(sampleSize); ByteBuffer videoBuf = ByteBuffer.allocate(sampleSize);
ByteBuffer audioBuf = ByteBuffer.allocate(sampleSize); ByteBuffer audioBuf = ByteBuffer.allocate(sampleSize);
MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo(); MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo();
@ -412,7 +412,6 @@ public class DownloadRedditVideoService extends Service {
videoBufferInfo.offset = offset; videoBufferInfo.offset = offset;
videoBufferInfo.size = videoExtractor.readSampleData(videoBuf, offset); videoBufferInfo.size = videoExtractor.readSampleData(videoBuf, offset);
if (videoBufferInfo.size < 0 || audioBufferInfo.size < 0) { if (videoBufferInfo.size < 0 || audioBufferInfo.size < 0) {
sawEOS = true; sawEOS = true;
videoBufferInfo.size = 0; videoBufferInfo.size = 0;
@ -441,8 +440,10 @@ public class DownloadRedditVideoService extends Service {
} }
} }
muxer.stop(); try {
muxer.release(); muxer.stop();
muxer.release();
} catch (IllegalStateException ignore) {}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;

View File

@ -56,7 +56,7 @@ public class GlideImageGetter implements Html.ImageGetter {
BitmapDrawablePlaceholder drawable = new BitmapDrawablePlaceholder(textSize); BitmapDrawablePlaceholder drawable = new BitmapDrawablePlaceholder(textSize);
Context context = container.get().getContext(); Context context = container.get().getContext();
if (!(context instanceof Activity && ((Activity) context).isFinishing())) { if (!(context instanceof Activity && ((Activity) context).isDestroyed())) {
container.get().post(() -> Glide.with(context) container.get().post(() -> Glide.with(context)
.asBitmap() .asBitmap()
.load(source) .load(source)
@ -118,7 +118,9 @@ public class GlideImageGetter implements Html.ImageGetter {
@Override @Override
public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) { public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
setDrawable(new BitmapDrawable(container.get().getResources(), bitmap)); if (container != null) {
setDrawable(new BitmapDrawable(container.get().getResources(), bitmap));
}
} }
@Override @Override

View File

@ -28,6 +28,8 @@
android:id="@+id/image_view_view_image_activity" android:id="@+id/image_view_view_image_activity"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:gest_fillViewport="true" /> app:gest_fillViewport="true" />
<LinearLayout <LinearLayout

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraintLayout" android:id="@+id/constraintLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -15,7 +16,10 @@
<com.alexvasilkov.gestures.views.GestureImageView <com.alexvasilkov.gestures.views.GestureImageView
android:id="@+id/image_view_view_imgur_image_fragment" android:id="@+id/image_view_view_imgur_image_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:gest_fillViewport="true" />
<LinearLayout <LinearLayout
android:id="@+id/load_image_error_linear_layout_view_imgur_image_fragment" android:id="@+id/load_image_error_linear_layout_view_imgur_image_fragment"