Skip to content

Commit

Permalink
1.修复了后台进程杀死Activity的数据的缓存问题
Browse files Browse the repository at this point in the history
  • Loading branch information
keep2iron committed Aug 13, 2019
1 parent 99aeded commit 0c94eac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
*
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}
}
Expand All @@ -90,7 +91,7 @@ class MediaStoreCompat(
fragment.startActivityForResult(captureIntent, requestCode)
} else {
mContext.get()
?.startActivityForResult(captureIntent, requestCode)
?.startActivityForResult(captureIntent, requestCode)
}
}
}
Expand All @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 0c94eac

Please sign in to comment.