diff --git a/core/src/main/java/io/github/keep2iron/pejoy/ui/AlbumFragment.kt b/core/src/main/java/io/github/keep2iron/pejoy/ui/AlbumFragment.kt index fb8f0d6..ef28ca7 100644 --- a/core/src/main/java/io/github/keep2iron/pejoy/ui/AlbumFragment.kt +++ b/core/src/main/java/io/github/keep2iron/pejoy/ui/AlbumFragment.kt @@ -40,6 +40,7 @@ import io.github.keep2iron.pejoy.ui.view.PejoyCheckRadioView import io.github.keep2iron.pejoy.utilities.MediaStoreCompat import io.github.keep2iron.pejoy.utilities.getThemeColor import java.util.ArrayList +import kotlin.LazyThreadSafetyMode.NONE /** * @@ -80,7 +81,7 @@ open class AlbumFragment : Fragment(), View.OnClickListener { private val spec = SelectionSpec.instance - private val mediaStoreCompat: MediaStoreCompat by lazy { + private val mediaStoreCompat: MediaStoreCompat by lazy(NONE) { MediaStoreCompat(requireActivity(), this) } @@ -120,6 +121,7 @@ open class AlbumFragment : Fragment(), View.OnClickListener { model.onCreateViewFragment(savedInstanceState) + mediaStoreCompat.onCreate(savedInstanceState) if (spec.captureStrategy != null) { mediaStoreCompat.setCaptureStrategy(spec.captureStrategy!!) } else { @@ -263,7 +265,7 @@ open class AlbumFragment : Fragment(), View.OnClickListener { } recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { - Log.d("tag","newState : ${newState}") + Log.d("tag", "newState : ${newState}") when (newState) { RecyclerView.SCROLL_STATE_IDLE -> { SelectionSpec.instance.requireImageEngine().resume(requireContext().applicationContext) @@ -341,6 +343,7 @@ open class AlbumFragment : Fragment(), View.OnClickListener { override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) model.onSaveInstanceState(outState) + mediaStoreCompat.onSaveInstanceState(outState) } override fun onDestroy() { diff --git a/core/src/main/java/io/github/keep2iron/pejoy/utilities/MediaStoreCompat.kt b/core/src/main/java/io/github/keep2iron/pejoy/utilities/MediaStoreCompat.kt index ec911b1..04dffa1 100644 --- a/core/src/main/java/io/github/keep2iron/pejoy/utilities/MediaStoreCompat.kt +++ b/core/src/main/java/io/github/keep2iron/pejoy/utilities/MediaStoreCompat.kt @@ -21,6 +21,7 @@ import android.content.Intent import android.content.pm.PackageManager import android.net.Uri import android.os.Build +import android.os.Bundle import android.os.Environment import android.provider.MediaStore import androidx.core.content.FileProvider @@ -65,22 +66,22 @@ class MediaStoreCompat( if (photoFile != null) { currentPhotoPath = photoFile.absolutePath currentPhotoUri = FileProvider.getUriForFile( - context, - mCaptureStrategy!!.authority, - photoFile + context, + mCaptureStrategy!!.authority, + photoFile ) captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, currentPhotoUri) captureIntent.addFlags( - Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION ) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { val resInfoList = context.packageManager - .queryIntentActivities(captureIntent, PackageManager.MATCH_DEFAULT_ONLY) + .queryIntentActivities(captureIntent, PackageManager.MATCH_DEFAULT_ONLY) for (resolveInfo in resInfoList) { val packageName = resolveInfo.activityInfo.packageName context.grantUriPermission( - packageName, currentPhotoUri, - Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION + packageName, currentPhotoUri, + Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION ) } } @@ -90,7 +91,7 @@ class MediaStoreCompat( fragment.startActivityForResult(captureIntent, requestCode) } else { mContext.get() - ?.startActivityForResult(captureIntent, requestCode) + ?.startActivityForResult(captureIntent, requestCode) } } } @@ -103,14 +104,14 @@ class MediaStoreCompat( val storageDir: File? if (mCaptureStrategy!!.isPublic) { storageDir = Environment.getExternalStoragePublicDirectory( - Environment.DIRECTORY_PICTURES + Environment.DIRECTORY_PICTURES ) if (!storageDir!!.exists()) { storageDir.mkdirs() } } else { storageDir = mContext.get() - ?.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + ?.getExternalFilesDir(Environment.DIRECTORY_PICTURES) } // Avoid joining path components manually @@ -134,18 +135,33 @@ class MediaStoreCompat( fun insertAlbum( context: Context, imagePath: String, - onScanerComplete: (() -> Unit)? = null + onScannerComplete: (() -> Unit)? = null ) { val file = File(imagePath) MediaStore.Images.Media.insertImage( - context.contentResolver, - file.absolutePath, file.nameWithoutExtension, - null + context.contentResolver, + file.absolutePath, file.nameWithoutExtension, + null ) - CaptureMediaScanner(context, imagePath, onScanerComplete) + CaptureMediaScanner(context, imagePath, onScannerComplete) + } + + fun onSaveInstanceState(outState: Bundle) { + outState.putParcelable(STATE_PHOTO_URI, currentPhotoUri) + outState.putString(STATE_PHOTO_PATH, currentPhotoPath) + } + + fun onCreate(bundle: Bundle?){ + if(bundle != null){ + currentPhotoUri = bundle.getParcelable(STATE_PHOTO_URI) + currentPhotoPath = bundle.getString(STATE_PHOTO_PATH)!! + } } companion object { + const val STATE_PHOTO_URI = "photo_uri" + + const val STATE_PHOTO_PATH = "photo_path" /** * Checks whether the device has a camera feature or not.