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 947df74 commit ad07736
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
3 changes: 3 additions & 0 deletions module_camera/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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 @@ -28,4 +29,6 @@
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />

<!-- <application android:requestLegacyExternalStorage="true" />-->
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package com.ailiwean.core.view
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.annotation.SuppressLint
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ImageDecoder
import android.graphics.PointF
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
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
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -193,6 +194,12 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri

var busHandle: Handler? = null

private fun initBusHandle() {
val handlerThread = HandlerThread(System.currentTimeMillis().toString())
handlerThread.start()
busHandle = Handler(handlerThread.looper)
}

protected fun parseFile(filePath: String) {

if (!checkPermissionRW())
Expand All @@ -207,7 +214,7 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri

busHandle?.post {
val bitmap: Bitmap = (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(file))
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, getMediaUriFromPath(context, filePath)))
.copy(Bitmap.Config.RGB_565, false)
} else
BitmapFactory.decodeFile(filePath))
Expand All @@ -216,13 +223,9 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri
}
}

private fun initBusHandle() {
val handlerThread = HandlerThread(System.currentTimeMillis().toString())
handlerThread.start()
busHandle = Handler(handlerThread.looper)
}

protected fun parseBitmap(bitmap: Bitmap) {
protected fun parseBitmap(bitmap: Bitmap?) {
if (bitmap == null)
return
if (busHandle == null)
initBusHandle()
busHandle?.post {
Expand Down Expand Up @@ -257,4 +260,20 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri
}
}
}

open fun getMediaUriFromPath(context: Context, path: String): Uri {
val mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val cursor: Cursor? = context.contentResolver.query(mediaUri,
null,
MediaStore.Images.Media.DISPLAY_NAME + "= ?", arrayOf(path.substring(path.lastIndexOf("/") + 1)),
null)
var uri: Uri? = null
cursor?.let {
it.moveToFirst()
uri = ContentUris.withAppendedId(mediaUri,
it.getLong(it.getColumnIndex(MediaStore.Images.Media._ID)))
}
cursor?.close()
return uri ?: Uri.EMPTY
}
}

0 comments on commit ad07736

Please sign in to comment.