From 9f146f3c6d6de04fbf31c10ba911519880fc4d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20M=C3=A9tais?= Date: Thu, 24 Jan 2019 11:07:14 +0100 Subject: [PATCH] Fix crash while blurring covers Fix #735 --- .../org/videolan/vlc/gui/helpers/UiTools.java | 69 ++++++++++--------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java index 273bfcd60..a4f636a35 100644 --- a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java +++ b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java @@ -32,7 +32,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.TypedArray; -import androidx.databinding.BindingAdapter; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; @@ -40,19 +39,11 @@ import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; -import android.preference.PreferenceManager; import android.renderscript.Allocation; import android.renderscript.Element; +import android.renderscript.RSInvalidStateException; import android.renderscript.RenderScript; import android.renderscript.ScriptIntrinsicBlur; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; -import com.google.android.material.snackbar.Snackbar; -import androidx.fragment.app.FragmentActivity; -import androidx.core.content.ContextCompat; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; import android.text.Html; import android.text.TextUtils; import android.view.DragAndDropPermissions; @@ -70,11 +61,12 @@ import android.view.inputmethod.InputMethodManager; import android.widget.ImageView; import android.widget.TextView; +import com.google.android.material.snackbar.Snackbar; + import org.videolan.libvlc.util.AndroidUtil; import org.videolan.medialibrary.Medialibrary; import org.videolan.medialibrary.media.MediaLibraryItem; import org.videolan.medialibrary.media.MediaWrapper; -import org.videolan.vlc.BuildConfig; import org.videolan.vlc.MediaParsingService; import org.videolan.vlc.R; import org.videolan.vlc.VLCApplication; @@ -90,6 +82,15 @@ import org.videolan.vlc.viewmodels.SortableModel; import java.util.List; import java.util.Locale; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; +import androidx.databinding.BindingAdapter; +import androidx.fragment.app.FragmentActivity; + public class UiTools { private static final String TAG = "VLC/UiTools"; @@ -310,37 +311,39 @@ public class UiTools { @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) public static Bitmap blurBitmap(Bitmap bitmap, float radius) { - if (bitmap == null || bitmap.getConfig() == null) - return null; + if (bitmap == null || bitmap.getConfig() == null) return null; + try { + //Let's create an empty bitmap with the same size of the bitmap we want to blur + final Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); - //Let's create an empty bitmap with the same size of the bitmap we want to blur - Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); + //Instantiate a new Renderscript + final RenderScript rs = RenderScript.create(VLCApplication.getAppContext()); - //Instantiate a new Renderscript - RenderScript rs = RenderScript.create(VLCApplication.getAppContext()); + //Create an Intrinsic Blur Script using the Renderscript + ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); - //Create an Intrinsic Blur Script using the Renderscript - ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); + //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps + final Allocation allIn = Allocation.createFromBitmap(rs, bitmap); + final Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); - //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps - Allocation allIn = Allocation.createFromBitmap(rs, bitmap); - Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); + //Set the radius of the blur + blurScript.setRadius(radius); - //Set the radius of the blur - blurScript.setRadius(radius); + //Perform the Renderscript + blurScript.setInput(allIn); + blurScript.forEach(allOut); - //Perform the Renderscript - blurScript.setInput(allIn); - blurScript.forEach(allOut); + //Copy the final bitmap created by the out Allocation to the outBitmap + allOut.copyTo(outBitmap); - //Copy the final bitmap created by the out Allocation to the outBitmap - allOut.copyTo(outBitmap); + //After finishing everything, we destroy the Renderscript. + rs.destroy(); - //After finishing everything, we destroy the Renderscript. - rs.destroy(); - - return outBitmap; + return outBitmap; + } catch (RSInvalidStateException ignored) { + return null; + } } public static void updateSortTitles(MediaBrowserFragment sortable) {