修正FileProvider取得的contentUri,在下次使用時失效的問題
This commit is contained in:
parent
2eebb63122
commit
003bbb9e63
@ -9,6 +9,7 @@ import android.os.Bundle
|
|||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
import android.webkit.MimeTypeMap
|
||||||
import android.widget.AdapterView
|
import android.widget.AdapterView
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
@ -20,6 +21,7 @@ import com.ray650128.floatingwindow.utils.DensityUtil
|
|||||||
import com.ray650128.floatingwindow.utils.FloatingWindowHelperUtils
|
import com.ray650128.floatingwindow.utils.FloatingWindowHelperUtils
|
||||||
import com.ray650128.floatingwindow.utils.PreferenceUtil
|
import com.ray650128.floatingwindow.utils.PreferenceUtil
|
||||||
import com.ray650128.floatingwindow.view.ValueEditor
|
import com.ray650128.floatingwindow.view.ValueEditor
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
|
||||||
class SettingActivity : AppCompatActivity() {
|
class SettingActivity : AppCompatActivity() {
|
||||||
@ -52,15 +54,16 @@ class SettingActivity : AppCompatActivity() {
|
|||||||
private val galleryLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
private val galleryLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||||
if (result.resultCode == Activity.RESULT_OK) {
|
if (result.resultCode == Activity.RESULT_OK) {
|
||||||
val data = result.data
|
val data = result.data
|
||||||
imageUri = data?.data
|
val returnUri = data?.data ?: return@registerForActivityResult
|
||||||
|
imageUri = saveImageToCache(returnUri)
|
||||||
Glide.with(this).load(imageUri).into(binding.imgPreview)
|
Glide.with(this).load(imageUri).into(binding.imgPreview)
|
||||||
FloatingWindowHelperUtils.setIcon(windowIcon, imageUri)
|
FloatingWindowHelperUtils.setIcon(windowIcon, Uri.parse(imageUri))
|
||||||
//workingCheckItemAdapter.setImageUri(currentPosition, currentUri)
|
//workingCheckItemAdapter.setImageUri(currentPosition, currentUri)
|
||||||
//checkPassButton()
|
//checkPassButton()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var imageUri: Uri? = null
|
private var imageUri: String? = null
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@ -72,7 +75,7 @@ class SettingActivity : AppCompatActivity() {
|
|||||||
yOffset = PreferenceUtil.yOffset
|
yOffset = PreferenceUtil.yOffset
|
||||||
windowGravity = FloatingWindowHelperUtils.GravityType.fromInt(PreferenceUtil.gravity)
|
windowGravity = FloatingWindowHelperUtils.GravityType.fromInt(PreferenceUtil.gravity)
|
||||||
windowIcon = FloatingWindowHelperUtils.IconType.fromInt(PreferenceUtil.icon)
|
windowIcon = FloatingWindowHelperUtils.IconType.fromInt(PreferenceUtil.icon)
|
||||||
imageUri = if (PreferenceUtil.iconPath.isNullOrEmpty()) null else {
|
if (!PreferenceUtil.iconPath.isNullOrEmpty()) {
|
||||||
Uri.parse(PreferenceUtil.iconPath)
|
Uri.parse(PreferenceUtil.iconPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +97,7 @@ class SettingActivity : AppCompatActivity() {
|
|||||||
onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||||
windowIcon = FloatingWindowHelperUtils.IconType.fromIndex(position)
|
windowIcon = FloatingWindowHelperUtils.IconType.fromIndex(position)
|
||||||
FloatingWindowHelperUtils.setIcon(windowIcon, imageUri)
|
FloatingWindowHelperUtils.setIcon(windowIcon, Uri.parse(imageUri))
|
||||||
if (windowIcon == FloatingWindowHelperUtils.IconType.CUSTOM) {
|
if (windowIcon == FloatingWindowHelperUtils.IconType.CUSTOM) {
|
||||||
imgPreview.isVisible = true
|
imgPreview.isVisible = true
|
||||||
btnPickPhoto.isVisible = true
|
btnPickPhoto.isVisible = true
|
||||||
@ -205,7 +208,7 @@ class SettingActivity : AppCompatActivity() {
|
|||||||
PreferenceUtil.yOffset = yOffset
|
PreferenceUtil.yOffset = yOffset
|
||||||
PreferenceUtil.gravity = windowGravity.type
|
PreferenceUtil.gravity = windowGravity.type
|
||||||
PreferenceUtil.icon = windowIcon.value
|
PreferenceUtil.icon = windowIcon.value
|
||||||
PreferenceUtil.iconPath = imageUri?.toString()
|
PreferenceUtil.iconPath = imageUri
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,4 +233,23 @@ class SettingActivity : AppCompatActivity() {
|
|||||||
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
|
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
|
||||||
galleryLauncher.launch(intent)
|
galleryLauncher.launch(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun saveImageToCache(uri: Uri?): String? {
|
||||||
|
if (uri == null) return null
|
||||||
|
val inputStream = contentResolver.openInputStream(uri)
|
||||||
|
inputStream?.use { input ->
|
||||||
|
val extension = getFileExtension(uri.toString())
|
||||||
|
val outputFile = File(cacheDir, "custom.$extension")
|
||||||
|
outputFile.outputStream().use { output ->
|
||||||
|
input.copyTo(output)
|
||||||
|
}
|
||||||
|
return outputFile.absolutePath
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getFileExtension(uri: String): String {
|
||||||
|
return MimeTypeMap.getSingleton().getExtensionFromMimeType(contentResolver.getType(Uri.parse(uri)))
|
||||||
|
?: "jpg" // Default extension
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user