[修复] 书籍网络封面大图查看或保存 #419

This commit is contained in:
HapeLee
2025-12-05 00:30:37 +08:00
parent 1b20ac4be6
commit 8df2f009a9
2 changed files with 39 additions and 38 deletions
@@ -1,8 +1,11 @@
package io.legado.app.ui.widget.dialog
import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy
import com.bumptech.glide.request.RequestOptions
@@ -15,7 +18,10 @@ import io.legado.app.help.glide.OkHttpModelLoader
import io.legado.app.model.BookCover
import io.legado.app.model.ImageProvider
import io.legado.app.model.ReadBook
import io.legado.app.utils.ImageSaveUtils.saveImageToGallery
import io.legado.app.utils.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding
import java.io.ByteArrayOutputStream
/**
* 显示图片
@@ -29,6 +35,13 @@ class PhotoDialog() : BaseBottomSheetDialogFragment(R.layout.dialog_photo_view)
}
}
override fun onStart() {
super.onStart()
val sheet = dialog
?.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
sheet?.layoutParams?.height = ViewGroup.LayoutParams.MATCH_PARENT
}
private val binding by viewBinding(DialogPhotoViewBinding::bind)
@SuppressLint("CheckResult")
@@ -59,6 +72,26 @@ class PhotoDialog() : BaseBottomSheetDialogFragment(R.layout.dialog_photo_view)
.downsample(DownsampleStrategy.NONE)
.into(binding.photoView)
}
binding.photoView.setOnLongClickListener {
saveCurrentImage()
true
}
}
private fun saveCurrentImage() {
val drawable = binding.photoView.drawable ?: return
val bitmap = (drawable as? BitmapDrawable)?.bitmap ?: return
val byteArray = ByteArrayOutputStream().use { stream ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
stream.toByteArray()
}
val success = saveImageToGallery(requireContext(), byteArray)
toastOnUi(
if (success) "已保存到相册" else "保存失败"
)
}
}
+6 -38
View File
@@ -1,47 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="16dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@+id/header_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:paddingHorizontal="16dp"
android:paddingBottom="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.bottomsheet.BottomSheetDragHandleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingVertical="16dp"
android:minHeight="0dp"
android:layout_gravity="center_horizontal" />
<TextView
android:id="@+id/bottom_sheet_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/photo"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
android:layout_height="match_parent"
android:paddingHorizontal="16dp">
<io.legado.app.ui.widget.image.PhotoView
android:id="@+id/photo_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/header_layout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:scaleType="centerInside" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
</androidx.constraintlayout.widget.ConstraintLayout>