Skip to content

Commit

Permalink
1.添加单独的拍照选择
Browse files Browse the repository at this point in the history
2.添加示例代码和ReadMe
  • Loading branch information
keep2iron committed Aug 14, 2019
1 parent 0c94eac commit 1154cf8
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 45 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Image](/image/banner.png)
![Image](image/banner.png)

# Pejoy
![Release](https://api.bintray.com/packages/keep2iron/maven/pejoy-core/images/download.svg) ![BuildStatus](https://travis-ci.org/keep2iron/pejoy.svg?branch=master)
Expand Down Expand Up @@ -93,6 +93,31 @@ Pejoy.create(this)
}
````

##### Capture

````kotlin
Pejoy.create(this)
.capture()
.originalEnable(true)
.toObservable()
.weatherCompressImage(requireContext())
.subscribe {
imageResultBuilder.append("[\n")
it.forEach { uri ->
imageResultBuilder.apply {
append(uri)
if (uri != it.last()) {
append("\n")
} else {
append("\n]\n")
}
}
}
tvImageResult.text = imageResultBuilder.toString()
Log.d("keep2iron", it.toString() + "this : " + this.hashCode())
}
````

## ProGuard

No need......
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/io/github/keep2iron/pejoy/app/ChildFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.fragment.app.Fragment
import io.github.keep2iron.pejoy.MimeType
import io.github.keep2iron.pejoy.Pejoy
import io.github.keep2iron.pejoy.engine.FrescoImageEngine
import io.github.keep2iron.pejoy.utilities.extractStringPath
import keep2iron.github.io.compress.weatherCompressImage

class ChildFragment : Fragment() {
Expand Down Expand Up @@ -60,6 +61,28 @@ class ChildFragment : Fragment() {
Log.d("keep2iron", it.toString() + "this : " + this.hashCode())
}
}
view.findViewById<View>(R.id.btnTakPhoto).setOnClickListener {
Pejoy.create(this)
.capture()
.originalEnable(true)
.toObservable()
.weatherCompressImage(requireContext())
.subscribe {
imageResultBuilder.append("[\n")
it.forEach { uri ->
imageResultBuilder.apply {
append(uri)
if (uri != it.last()) {
append("\n")
} else {
append("\n]\n")
}
}
}
tvImageResult.text = imageResultBuilder.toString()
Log.d("keep2iron", it.toString() + "this : " + this.hashCode())
}
}
}

}
24 changes: 17 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,45 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical"
tools:context=".MainActivity"
>

<ImageView
android:layout_gravity="center_horizontal"
android:src="@mipmap/logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:src="@mipmap/logo"
/>

<Button
android:background="@color/colorPrimaryDark"
android:id="@+id/btnGetImages"
android:textColor="@android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:text="start get pejoy!"
android:textColor="@android:color/white"
/>

<Button
android:id="@+id/btnTakPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/colorPrimaryDark"
android:text="start take Photo!"
android:textColor="@android:color/white"
/>

<TextView
android:id="@+id/tvImageResult"
android:textColor="@android:color/white"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textColor="@android:color/white"
/>

</LinearLayout>
4 changes: 2 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
implementation deps.kotlin.stdlib_jdk7

implementation deps.rxjava2
implementation 'io.github.keep2iron:rx-result:0.0.1'
implementation 'io.github.keep2iron:rx-result:0.0.2'

testImplementation deps.test.junit
androidTestImplementation deps.test.runner
Expand All @@ -58,7 +58,7 @@ publish {
userOrg = 'keep2iron'
groupId = 'io.github.keep2iron'
artifactId = 'pejoy-core'
publishVersion = '0.0.2'
publishVersion = '0.0.3'
desc = 'image picker,base on matisse'
website = 'https://keep2iron.github.io'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.github.keep2iron.pejoy

import android.content.Intent
import android.net.Uri
import android.os.Build
import io.github.keep2iron.pejoy.internal.entity.CaptureStrategy
import io.github.keep2iron.pejoy.utilities.MediaStoreCompat
import io.github.keep2iron.rxresult.RxResult
import io.reactivex.Observable

class CaptureSelectionCreator
internal constructor(
private val pejoy: Pejoy
) {

private var captureStrategy: CaptureStrategy? = null

private var originalable: Boolean = true

/**
* Capture strategy provided for the location to save photos including internal and external
* storage and also a authority for [androidx.core.content.FileProvider].
*
* @param captureStrategy [CaptureStrategy], needed only when capturing is enabled.
* @return [SelectionCreator] for fluent API.
*/
fun captureStrategy(captureStrategy: CaptureStrategy): CaptureSelectionCreator {
this.captureStrategy = captureStrategy
return this
}

/**
* Show a original photo check options.Let users decide whether use original photo after select
*
* @param enable Whether to enable original photo or not
* @return [SelectionCreator] for fluent API.
*/
fun originalEnable(
enable: Boolean
): CaptureSelectionCreator {
originalable = enable
return this
}

fun toObservable(): Observable<Intent> {
val requestActivity = pejoy.getActivity()
val requestFragment = pejoy.getFragment()

val activityCtx = requestActivity ?: requestFragment?.requireActivity()
?: throw IllegalArgumentException("activity or fragment is not null")
val mediaStoreCompat = MediaStoreCompat(activityCtx)
val capture = captureStrategy ?: CaptureStrategy(
true,
activityCtx.applicationContext.packageName + ".provider.PejoyProvider"
)
mediaStoreCompat.setCaptureStrategy(capture)

return when {
requestActivity != null -> RxResult(requestActivity)
requestFragment != null -> RxResult(requestFragment)
else -> throw IllegalArgumentException("activity or fragment is not null")
}
.prepare(mediaStoreCompat.buildCaptureIntent(activityCtx))
.requestForResult()
.filter {
it.result
}
.map {
val contentUri = mediaStoreCompat.getCurrentPhotoUri()
val path = mediaStoreCompat.getCurrentPhotoPath()
val selected = ArrayList<Uri>()
selected.add(contentUri)
val selectedPath = ArrayList<String>()
selectedPath.add(path)
val result = Intent()
result.putExtra(Pejoy.EXTRA_RESULT_ORIGIN_ENABLE, originalable)
result.putParcelableArrayListExtra(Pejoy.EXTRA_RESULT_SELECTION, selected)
result.putStringArrayListExtra(Pejoy.EXTRA_RESULT_SELECTION_PATH, selectedPath)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
activityCtx.revokeUriPermission(
contentUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
)
}
result
}
}

}
4 changes: 4 additions & 0 deletions core/src/main/java/io/github/keep2iron/pejoy/Pejoy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ class Pejoy {
return SelectionCreator(this, mimeTypes.toSet(), mediaTypeExclusive)
}

fun capture(): CaptureSelectionCreator {
return CaptureSelectionCreator(this)
}

fun getActivity(): FragmentActivity? {
return mActivity?.get()
}
Expand Down
32 changes: 0 additions & 32 deletions core/src/main/java/io/github/keep2iron/pejoy/SelectionCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -321,37 +321,5 @@ internal constructor(
.map {
it.data
}

// val activity = pejoy.getActivity() ?: return Observable.empty()
// val fragment = pejoy.getFragment()
//
// val fragmentManager = if (fragment != null) {
// val fragmentActivity = fragment.activity
// if (fragmentActivity == null) {
// return Observable.error(IllegalArgumentException("activity is null!!"))
// } else {
// fragmentActivity.supportFragmentManager
// }
// } else {
// activity.supportFragmentManager
// }
//
// hockFragment = findFragmentByTag(fragmentManager)
// val newInstance = hockFragment == null
//
// if (newInstance) {
// hockFragment = HockFragment.newInstance(
// Pejoy.REQUEST_CODE
// )
//
// val transaction = fragmentManager.beginTransaction()
// transaction.add(hockFragment!!, HockFragment::class.java.simpleName)
// transaction.commitNow()
// } else {
// hockFragment!!.startActivityForResult()
// }
// hockFragment!!.restoreObservable()
//
// return hockFragment!!.toObservable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,40 @@ class MediaStoreCompat(
mCaptureStrategy = strategy
}

internal fun buildCaptureIntent(
context: Context
): Intent {
val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (captureIntent.resolveActivity(context.packageManager) != null) {
val photoFile: File? = createImageFile()

if (photoFile != null) {
currentPhotoPath = photoFile.absolutePath
currentPhotoUri = FileProvider.getUriForFile(
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
)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
val resInfoList = context.packageManager
.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
)
}
}
}
}
return captureIntent
}

fun dispatchCaptureIntent(
context: Context,
requestCode: Int
Expand Down Expand Up @@ -151,8 +185,8 @@ class MediaStoreCompat(
outState.putString(STATE_PHOTO_PATH, currentPhotoPath)
}

fun onCreate(bundle: Bundle?){
if(bundle != null){
fun onCreate(bundle: Bundle?) {
if (bundle != null) {
currentPhotoUri = bundle.getParcelable(STATE_PHOTO_URI)
currentPhotoPath = bundle.getString(STATE_PHOTO_PATH)!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.net.Uri
import io.github.keep2iron.pejoy.Pejoy
import io.reactivex.Observable

fun Observable<Intent>.extractStringPath(): Observable<List<String>> {
fun Observable<Intent>.extractStringPath(): Observable<ArrayList<String>> {
return map {
it.getStringArrayListExtra(Pejoy.EXTRA_RESULT_SELECTION_PATH)
}
Expand Down

0 comments on commit 1154cf8

Please sign in to comment.