Skip to content

Commit

Permalink
适配android 10文件沙盒模式
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiwean committed Aug 4, 2020
1 parent eef53bf commit 18804b1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 46 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ android {
keyPassword 'shuai1234'
}
}
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
applicationId "com.android.NBZxing"
minSdkVersion 19
targetSdkVersion 28
versionCode 8
versionName "1.7"
targetSdkVersion 29
versionCode 9
versionName "1.9"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
signingConfig signingConfigs.release
compileOptions {
Expand Down
14 changes: 0 additions & 14 deletions app/src/main/java/com/android/NBZxing/CusZxingView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,6 @@ class CusZxingView @JvmOverloads constructor(context: Context, attributeSet: Att
}


fun checkPermissionRW(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
context.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

context.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

} else {
return true
}
}


fun requstPermissionRW() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
(context as? Activity)?.requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE), 200)
Expand Down
2 changes: 1 addition & 1 deletion module_camera/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.ailiwean'

android {
compileSdkVersion 28
compileSdkVersion 29
resourcePrefix "camera_"
defaultConfig {
minSdkVersion 17
Expand Down
3 changes: 0 additions & 3 deletions module_camera/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.cameraview">


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Expand All @@ -29,6 +28,4 @@
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />

<application />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ImageDecoder
import android.graphics.PointF
import android.graphics.drawable.BitmapDrawable
import android.os.Build
import android.os.Handler
import android.os.HandlerThread
import android.os.Message
import android.provider.MediaStore
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
Expand Down Expand Up @@ -190,16 +194,24 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri
var busHandle: Handler? = null

protected fun parseFile(filePath: String) {

if (!checkPermissionRW())
return

val file = File(filePath)
if (!file.exists()) {
if (!file.exists())
return
}

if (busHandle == null)
initBusHandle()

busHandle?.post {
val bitmap = BitmapFactory.decodeFile(filePath)
val bitmap: Bitmap = (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(file))
.copy(Bitmap.Config.RGB_565, false)
} else
BitmapFactory.decodeFile(filePath))
?: return@post
parseBitmap(bitmap)
}
}
Expand All @@ -213,25 +225,34 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri
protected fun parseBitmap(bitmap: Bitmap) {
if (busHandle == null)
initBusHandle()

busHandle?.post {
val source = BitmapLuminanceSource(bitmap)

var result = CustomMultiFormatReader.getInstance()
.decode(BinaryBitmap(GlobalHistogramBinarizer(source)))

if (result == null)
result = CustomMultiFormatReader.getInstance()
.decode(BinaryBitmap(HybridBinarizer(source)))

if (result != null) {
mainHand.post {
resultBackFile(result.text)
scanSucHelper()
bitmap.apply {
if (config != Bitmap.Config.RGB_565
&& config != Bitmap.Config.ARGB_8888) {
if (isMutable)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
config = Bitmap.Config.RGB_565
} else {
copy(Bitmap.Config.RGB_565, false)
}
else
copy(Bitmap.Config.RGB_565, false)
}
} else {
mainHand.post {
resultBackFile("")
val source = BitmapLuminanceSource(this)
var result = CustomMultiFormatReader.getInstance()
.decode(BinaryBitmap(GlobalHistogramBinarizer(source)))
if (result == null)
result = CustomMultiFormatReader.getInstance()
.decode(BinaryBitmap(HybridBinarizer(source)))
if (result != null) {
mainHand.post {
resultBackFile(result.text)
scanSucHelper()
}
} else {
mainHand.post {
resultBackFile("")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
fun onComCreate() {
if (!isShoudCreateOpen)
return
if (checkPermission()) {
if (checkPermissionCamera()) {
openCamera()
} else {
requstPermission()
Expand All @@ -176,7 +176,7 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
if (isShoudCreateOpen) {
return
}
if (checkPermission())
if (checkPermissionCamera())
openCamera()
}

Expand Down Expand Up @@ -246,7 +246,7 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
cameraHandler.looper.quit()
}

fun checkPermission(): Boolean {
fun checkPermissionCamera(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
context.checkSelfPermission(
Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
Expand All @@ -255,9 +255,26 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
}
}

fun checkPermissionRW(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
context.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

context.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

} else {
return true
}
}

fun requstPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
(context as? Activity)?.requestPermissions(arrayOf(Manifest.permission.CAMERA), 200)
(context as? Activity)?.requestPermissions(arrayOf(Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
), 200)
}
}

}

0 comments on commit 18804b1

Please sign in to comment.