Open the front camera(kotlin) - android

I have the following code which opens the rear camera in SurfaceView when the activity is opened.
Now I want to know how to change this code to open the front camera instead of the rear camera
and I have given access to the front camera in the manifest.
And I don't want to go to camera using Intent
Thankful
class mirrorActivity : AppCompatActivity(), SurfaceHolder.Callback {
private lateinit var binding: ActivityMirrorBinding
private var surfaceHolder: SurfaceHolder? = null
private var camera: Camera? = null
private val neededPermissions = arrayOf(CAMERA)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMirrorBinding.inflate(layoutInflater)
setContentView(binding.root)
val result = checkPermission()
if (result) {
setupSurfaceHolder()
}
}
private fun checkPermission(): Boolean {
val currentAPIVersion = Build.VERSION.SDK_INT
if (currentAPIVersion >= Build.VERSION_CODES.M) {
val permissionsNotGranted = ArrayList<String>()
for (permission in neededPermissions) {
if (ContextCompat.checkSelfPermission( this, permission)
!=PackageManager.PERMISSION_GRANTED){
permissionsNotGranted.add(permission)
}
}
if (permissionsNotGranted.size > 0) {
var shouldShowAlert = false
for (permission in permissionsNotGranted) {
shouldShowAlert =
ActivityCompat.shouldShowRequestPermissionRationale(this, permission)
}
val arr = arrayOfNulls<String>(permissionsNotGranted.size)
val permissions = permissionsNotGranted.toArray(arr)
if (shouldShowAlert) {
showPermissionAlert(permissions)
} else {
requestPermissions(permissions)
}
return false
}
}
return true
}
private fun showPermissionAlert(permissions: Array<String?>) {
val alertBuilder = AlertDialog.Builder(this)
alertBuilder.setCancelable(true)
alertBuilder.setTitle("Permission Required")
alertBuilder.setMessage("You must grant permission to access camera and external storage to run this application")
alertBuilder.setPositiveButton(R.string.no_data) { _, _ -> requestPermissions(permissions) }
val alert = alertBuilder.create()
alert.show()
}
private fun requestPermissions(permissions: Array<String?>) {
ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
when (requestCode) {
REQUEST_CODE -> {
for (result in grantResults) {
if (result == PackageManager.PERMISSION_DENIED) {
//t
binding.showPermissionMsg.visibility = View.VISIBLE
return
}
}
setupSurfaceHolder() } }
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
private fun setupSurfaceHolder() {
surfaceHolder = binding.surfaceView.holder
binding.surfaceView.holder.addCallback(this)
}
override fun surfaceCreated(surfaceHolder: SurfaceHolder) {
startCamera()
}
private fun startCamera() {
camera = Camera.open()
camera!!.setDisplayOrientation(90)
try {
camera!!.setPreviewDisplay(surfaceHolder)
camera!!.startPreview()
} catch (e: IOException) {
e.printStackTrace()
}
}
override fun surfaceChanged(surfaceHolder: SurfaceHolder, i: Int, i1: Int, i2: Int) {
resetCamera()
}
private fun resetCamera() {
if (surfaceHolder!!.surface == null) {
return
}
camera!!.stopPreview()
try {
camera!!.setPreviewDisplay(surfaceHolder)
} catch (e: IOException) {
e.printStackTrace()
}
camera!!.startPreview()
}
override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) {
releaseCamera()
}
private fun releaseCamera() {
camera!!.stopPreview()
camera!!.release()
camera = null
}
companion object {
const val REQUEST_CODE = 100
}
}

You can use Camera.CameraInfo.CAMERA_FACING_FRONT for the front camera and Camera.CameraInfo.CAMERA_FACING_BACK for the back camera, you can use them in your code the following :
private int currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
or
private int currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
And set it like this :
Camera.open(currentCameraId)
Also, put the whole camera code in the onResume() method

Related

Kotlin Type mismatch: inferred type is Activity? but Context was expected ERROR after upgrading flutter to the 3.0.0

I am using beacon plugin. After I upgraded flutter to the version 3.0.0, it throwed this error:
Class 'BeaconsPlugin' is not abstract and does not implement abstract member public abstract fun onRequestPermissionsResult(p0: Int, p1: Array<(out) String!>, p2: IntArray): Boolean defined in io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
e: C:\Software\HR-Management-Localization\mapping_tool\packages\simple_beacons_flutter\android\src\main\kotlin\com\umair\beacons_plugin\BeaconsPlugin.kt: (66, 32): Type mismatch: inferred type is Activity? but Context was expected
e: C:\Software\HR-Management-Localization\mapping_tool\packages\simple_beacons_flutter\android\src\main\kotlin\com\umair\beacons_plugin\BeaconsPlugin.kt: (428, 5): 'onRequestPermissionsResult' overrides nothing
I've solved it by removing question marks from lines
permissions: Array<out String>?,
grantResults: IntArray?
to this
permissions: Array<out String>,
grantResults: IntArray
in onRequestPermissionsResult method.
And than it throwed a different error: BeaconsPlugin.kt: (66, 32): Type mismatch: inferred type is Activity? but Context was expected
I don't know kotlin therefore unable to solve this.
Here is my whole BeaconsPlugin.kt:
package com.umair.beacons_plugin
import android.Manifest
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import androidx.annotation.NonNull
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.*
import timber.log.Timber
/** BeaconsPlugin */
class BeaconsPlugin : FlutterPlugin, ActivityAware,
PluginRegistry.RequestPermissionsResultListener {
private var context: Context? = null
override fun onAttachedToEngine(#NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
Timber.i("onAttachedToEngine")
messenger = flutterPluginBinding.binaryMessenger
setUpPluginMethods(
flutterPluginBinding.applicationContext,
flutterPluginBinding.binaryMessenger
)
context = flutterPluginBinding.applicationContext
beaconHelper = BeaconHelper(flutterPluginBinding.applicationContext)
context?.let {
BeaconPreferences.init(it)
stopBackgroundService(it)
}
}
companion object {
private val TAG = "BeaconsPlugin"
private var REQUEST_LOCATION_PERMISSIONS = 1890
private var PERMISSION_REQUEST_BACKGROUND_LOCATION = 1891
private var channel: MethodChannel? = null
private var event_channel: EventChannel? = null
private var currentActivity: Activity? = null
private var beaconHelper: BeaconHelper? = null
private var defaultPermissionDialogTitle = "This app needs background location access"
private var defaultPermissionDialogMessage =
"Please grant location access so this app can detect beacons in the background."
#JvmStatic
internal var messenger: BinaryMessenger? = null
#JvmStatic
fun registerWith(registrar: PluginRegistry.Registrar) {
BeaconPreferences.init(registrar.context())
if (beaconHelper == null) {
this.beaconHelper = BeaconHelper(registrar.context())
}
val instance = BeaconsPlugin()
registrar.addRequestPermissionsResultListener(instance)
//requestPermission()
setUpPluginMethods(registrar.activity(), registrar.messenger())
}
#JvmStatic
fun registerWith(messenger: BinaryMessenger, context: Context) {
BeaconPreferences.init(context)
if (beaconHelper == null) {
this.beaconHelper = BeaconHelper(context)
}
val instance = BeaconsPlugin()
//requestPermission()
setUpPluginMethods(context, messenger)
}
#JvmStatic
fun registerWith(messenger: BinaryMessenger, beaconHelper: BeaconHelper, context: Context) {
BeaconPreferences.init(context)
this.beaconHelper = beaconHelper
val instance = BeaconsPlugin()
//requestPermission()
setUpPluginMethods(context, messenger)
}
#JvmStatic
private fun setUpPluginMethods(context: Context, messenger: BinaryMessenger) {
Timber.plant(Timber.DebugTree())
channel = MethodChannel(messenger, "beacons_plugin")
notifyIfPermissionsGranted(context)
channel?.setMethodCallHandler { call, result ->
when {
call.method == "startMonitoring" -> {
stopService = false
callBack?.startScanning()
result.success("Started scanning Beacons.")
}
call.method == "stopMonitoring" -> {
if (runInBackground) {
stopService = true
context.let {
stopBackgroundService(it)
}
}
callBack?.stopMonitoringBeacons()
result.success("Stopped scanning Beacons.")
}
call.method == "addRegion" -> {
callBack?.addRegion(call, result)
}
call.method == "clearRegions" -> {
callBack?.clearRegions(call, result)
}
call.method == "runInBackground" -> {
call.argument<Boolean>("background")?.let {
runInBackground = it
}
result.success("App will run in background? $runInBackground")
}
call.method == "clearDisclosureDialogShowFlag" -> {
call.argument<Boolean>("clearFlag")?.let {
if (it) {
clearPermissionDialogShownFlag()
result.success("clearDisclosureDialogShowFlag: Flag cleared!")
} else {
setPermissionDialogShown()
result.success("clearDisclosureDialogShowFlag: Flag Set!")
}
}
}
call.method == "setDisclosureDialogMessage" -> {
call.argument<String>("title")?.let {
defaultPermissionDialogTitle = it
}
call.argument<String>("message")?.let {
defaultPermissionDialogMessage = it
}
requestPermission()
result.success("Disclosure message Set: $defaultPermissionDialogMessage")
}
call.method == "addBeaconLayoutForAndroid" -> {
call.argument<String>("layout")?.let {
callBack?.addBeaconLayout(it)
result.success("Beacon layout added: $it")
}
}
call.method == "setForegroundScanPeriodForAndroid" -> {
var foregroundScanPeriod = 1100L
var foregroundBetweenScanPeriod = 0L
call.argument<Int>("foregroundScanPeriod")?.let {
if (it > foregroundScanPeriod) {
foregroundScanPeriod = it.toLong()
}
}
call.argument<Int>("foregroundBetweenScanPeriod")?.let {
if (it > foregroundBetweenScanPeriod) {
foregroundBetweenScanPeriod = it.toLong()
}
}
callBack?.setForegroundScanPeriod(
foregroundScanPeriod = foregroundScanPeriod,
foregroundBetweenScanPeriod = foregroundBetweenScanPeriod
)
result.success("setForegroundScanPeriod updated.")
}
call.method == "setBackgroundScanPeriodForAndroid" -> {
var backgroundScanPeriod = 1100L
var backgroundBetweenScanPeriod = 0L
call.argument<Int>("backgroundScanPeriod")?.let {
if (it > backgroundScanPeriod) {
backgroundScanPeriod = it.toLong()
}
}
call.argument<Int>("backgroundBetweenScanPeriod")?.let {
if (it > backgroundBetweenScanPeriod) {
backgroundBetweenScanPeriod = it.toLong()
}
}
callBack?.setBackgroundScanPeriod(
backgroundScanPeriod = backgroundScanPeriod,
backgroundBetweenScanPeriod = backgroundBetweenScanPeriod
)
result.success("setBackgroundScanPeriod updated.")
}
else -> result.notImplemented()
}
}
event_channel = EventChannel(messenger, "beacons_plugin_stream")
event_channel?.setStreamHandler(object : EventChannel.StreamHandler {
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
callBack?.setEventSink(events)
}
override fun onCancel(arguments: Any?) {
}
})
}
#JvmStatic
private fun notifyIfPermissionsGranted(context: Context) {
if (permissionsGranted(context)) {
doIfPermissionsGranted()
}
}
#JvmStatic
fun permissionsGranted(context: Context): Boolean {
return ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED
}
#JvmStatic
private fun doIfPermissionsGranted() {
Timber.i("doIfPermissionsGranted")
if (beaconHelper == null) {
return
}
this.callBack = beaconHelper
sendBLEScannerReadyCallback()
}
#JvmStatic
private fun requestPermission() {
if (areBackgroundScanPermissionsGranted()) {
requestLocationPermissions()
} else {
requestBackgroundPermission()
}
}
private fun requestLocationPermissions() {
if (!arePermissionsGranted()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
currentActivity?.let {
ActivityCompat.requestPermissions(
it,
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
),
REQUEST_LOCATION_PERMISSIONS
)
}
} else {
currentActivity?.let {
ActivityCompat.requestPermissions(
it,
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
),
REQUEST_LOCATION_PERMISSIONS
)
}
}
} else {
doIfPermissionsGranted()
}
} else {
doIfPermissionsGranted()
}
}
#JvmStatic
private fun requestBackgroundPermission() {
if (!isPermissionDialogShown()) {
currentActivity?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
//if (it.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
val builder: AlertDialog.Builder =
AlertDialog.Builder(it)
builder.setTitle(defaultPermissionDialogTitle)
builder.setMessage(defaultPermissionDialogMessage)
builder.setPositiveButton("Ok", null)
builder.setOnDismissListener {
setPermissionDialogShown()
requestLocationPermissions()
channel?.invokeMethod("isPermissionDialogShown", "true")
}
builder.show()
//}
}
}
}
}
#JvmStatic
private fun arePermissionsGranted(): Boolean {
currentActivity?.let {
return ContextCompat.checkSelfPermission(
it,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(
it,
Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED
}
return false
}
#JvmStatic
private fun areBackgroundScanPermissionsGranted(): Boolean {
currentActivity?.let {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContextCompat.checkSelfPermission(
it,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
) == PackageManager.PERMISSION_GRANTED
} else {
return true
}
}
return true
}
#JvmStatic
var runInBackground = false
#JvmStatic
var stopService = false
interface PluginImpl {
fun startScanning()
fun stopMonitoringBeacons()
fun addRegion(call: MethodCall, result: MethodChannel.Result)
fun clearRegions(call: MethodCall, result: MethodChannel.Result)
fun setEventSink(events: EventChannel.EventSink?)
fun addBeaconLayout(layout: String)
fun setForegroundScanPeriod(
foregroundScanPeriod: Long,
foregroundBetweenScanPeriod: Long
)
fun setBackgroundScanPeriod(
backgroundScanPeriod: Long,
backgroundBetweenScanPeriod: Long
)
}
private var callBack: PluginImpl? = null
fun sendBLEScannerReadyCallback() {
channel?.invokeMethod("scannerReady", "")
}
fun startBackgroundService(context: Context) {
if (runInBackground && !stopService) {
val serviceIntent1 = Intent(context, BeaconsDiscoveryService::class.java)
context.startService(serviceIntent1)
}
}
fun stopBackgroundService(context: Context) {
if (runInBackground && !stopService) {
val serviceIntent = Intent(context, BeaconsDiscoveryService::class.java)
context.stopService(serviceIntent)
}
}
}
override fun onDetachedFromEngine(#NonNull binding: FlutterPlugin.FlutterPluginBinding) {
currentActivity = null
channel?.setMethodCallHandler(null)
event_channel?.setStreamHandler(null)
if (!BeaconsPlugin.runInBackground)
beaconHelper?.stopMonitoringBeacons()
context?.let {
stopBackgroundService(it)
}
context = null
}
override fun onAttachedToActivity(activityPluginBinding: ActivityPluginBinding) {
currentActivity = activityPluginBinding.activity
BeaconPreferences.init(currentActivity)
activityPluginBinding.addRequestPermissionsResultListener(this)
//requestPermission()
if (arePermissionsGranted()) {
sendBLEScannerReadyCallback()
}
}
override fun onDetachedFromActivityForConfigChanges() {
Timber.i("onDetachedFromActivityForConfigChanges")
}
override fun onReattachedToActivityForConfigChanges(activityPluginBinding: ActivityPluginBinding) {
Timber.i("onReattachedToActivityForConfigChanges")
currentActivity = activityPluginBinding.activity
activityPluginBinding.addRequestPermissionsResultListener(this)
}
override fun onDetachedFromActivity() {
Timber.i("onDetachedFromActivity")
currentActivity = null
context?.let {
startBackgroundService(it)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
): Boolean {
if (requestCode == REQUEST_LOCATION_PERMISSIONS && grantResults?.isNotEmpty()!! && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
doIfPermissionsGranted()
return true
}
if (requestCode == PERMISSION_REQUEST_BACKGROUND_LOCATION && grantResults?.isNotEmpty()!! && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//setPermissionDialogShown()
//requestPermission()
return true
}
return false
}
}
i have save problem in deferent class and solved by edit this one
#JvmStatic
fun registerWith(registrar: PluginRegistry.Registrar) {
val instance = BackgroundSttPlugin()
registrar.addRequestPermissionsResultListener(instance)
requestRecordPermission()
// this one "registrar.context()" make my problem solved
setUpPluginMethods(registrar.context(), registrar.messenger())
}

How to list PDF files in Android?

I have the following code:
public ArrayList<File> getPDFs(File directory) {
ArrayList<File> pdfFileList = new ArrayList<>();
File[] paths = directory.listFiles();
if (!(paths == null)) {
for (File path : paths) {
if (path.isFile() && path.getName().endsWith(".pdf")) {
pdfFileList.add(path);
}
else if (path.isDirectory()) {
getPDFs(path);
}
}
}
return pdfFileList;
}
I also have the READ_EXTERNAL_STORAGE permission in my manifest file. When I call this function (e.g., getPDFs(Environment.getExternalStorageDirectory());) only folders are listed - I can't see any files. I downloaded a sample PDF with Chrome onto the emulator, but it doesn't get listed here. How can I list files (specifically PDF documents) in Android? I'm using Android 11.
Edit: The issue seems to be related to Chrome "owning" the file - my app didn't create the file and therefore cannot read it. I'm not sure how to get around this.
Edit 2: I believe I need to use the Storage Access Framework to access a directory, but again I'm not sure how.
Here is the source code try the following code you can get all pdf files from a mobile directory.
The code is in Kotlin Language hope you will understand. You should add permissions in manifest
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
LoaderManager.kt
class LoaderManager {
companion object {
val handler = CoroutineExceptionHandler { _, exception ->
exception.printStackTrace()
}
val pdfFileList = ArrayList<FileItem>()
fun getFilesLoader(
callback: () -> Unit
) {
GlobalScope.launch(Dispatchers.Main + handler) {
async(Dispatchers.IO + handler) {
getFiles(Environment.getExternalStorageDirectory())
}.await()
callback.invoke()
}
}
private fun getFiles(dir: File) {
try {
val pdfPattern = ".pdf"
val listFile = dir.listFiles()
if (listFile != null && listFile.isNotEmpty()) {
for (file in listFile) {
if (file.isDirectory) {
getFiles(file)
}
}
Arrays.sort(
listFile
) { file: File, t1: File ->
file.lastModified().compareTo(t1.lastModified())
}
for (file in listFile) {
if (file.exists() && file.name.endsWith(pdfPattern)) {
pdfFileList.add(FileItem(file))
}
}
}
} catch (ex: Exception) {
ex.printStackTrace()
}
}
fun isFilesListEmpty() = pdfFileList.isEmpty()
fun clearFilesData() {
pdfFileList.clear()
}
}
}
LoaderViewModel.kt
class LoaderViewModel : ViewModel() {
private val _filesLoad = MutableLiveData<Boolean>()
var isFilesLoaded: LiveData<Boolean> = Transformations.map(_filesLoad) { it }
fun setFilesLoaded(isLoaded: Boolean) {
_filesLoad.value = isLoaded
}
fun getGalleryFiles() {
if (LoaderManager.isFilesListEmpty()) {
LoaderManager.getFilesLoader {
setFilesLoaded(true)
}
} else {
setFilesLoaded(true)
}
}
}
FileItem.kt
data class FileItem(
var pdfFilePath: File
)
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var loaderViewModel: LoaderViewModel
private lateinit var mAdapter: AdapterFilesLoader
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
initViewModel()
createLoaderRecyclerView()
if (SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()){
loaderViewModel.getGalleryFiles()
}else{
showPermissionDialog()
}
} else {
if (checkReadWritePermission()) {
loaderViewModel.getGalleryFiles()
}else{
requestStoragePermission()
}
}
}
private fun initViewModel(){
loaderViewModel = ViewModelProvider(this).get(LoaderViewModel::class.java)
loaderViewModel.isFilesLoaded.observe(this) {
if (it){
binding.loadingProgressBar.visibility = View.GONE
Log.i("PDFTesting","${LoaderManager.pdfFileList.size}")
mAdapter.submitList(LoaderManager.pdfFileList)
if (LoaderManager.isFilesListEmpty()){
binding.noFilesLayout.visibility = View.VISIBLE
}
}else{
binding.loadingProgressBar.visibility = View.GONE
binding.noFilesLayout.visibility = View.GONE
}
}
}
private fun createLoaderRecyclerView() {
mAdapter = AdapterFilesLoader()
binding.recyclerview.adapter = mAdapter
binding.recyclerview.layoutManager = GridLayoutManager(this, 3)
mAdapter.setOnItemClickListener(object : OnItemClickListener {
override fun onItemClick(position: Int) {
showMessage(mAdapter.currentList[position].pdfFilePath.absolutePath)
}
})
}
private fun checkReadWritePermission(): Boolean {
return ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
}
private fun requestStoragePermission() {
ActivityCompat.requestPermissions(
this,
arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
),
STORAGE_PERMISSION
)
}
#RequiresApi(Build.VERSION_CODES.R)
private fun requestExternalStorageManager(){
try {
val mIntent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
mIntent.addCategory("android.intent.category.DEFAULT")
mIntent.data = Uri.parse(String.format("package:%s", packageName))
openActivityForResult(mIntent)
} catch (e: Exception) {
val mIntent = Intent()
mIntent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
openActivityForResult(mIntent)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (STORAGE_PERMISSION==requestCode){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
loaderViewModel.getGalleryFiles()
} else {
loaderViewModel.setFilesLoaded(false)
showMessage("Permission Denied!")
}
}
}
private var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()){
loaderViewModel.getGalleryFiles()
}else{
showPermissionDialog()
}
}
}
private fun openActivityForResult(mIntent:Intent) {
resultLauncher.launch(mIntent)
}
private fun showMessage(message:String){
Toast.makeText(this,message,Toast.LENGTH_SHORT).show()
}
override fun onDestroy() {
super.onDestroy()
LoaderManager.clearFilesData()
}
private fun showPermissionDialog() {
DialogUtils.permissionDialog(
this,
object : OnDialogPermissionClickListener {
override fun onDiscardClick() {
loaderViewModel.setFilesLoaded(false)
}
#RequiresApi(Build.VERSION_CODES.R)
override fun onProceedClick() {
requestExternalStorageManager()
}
})
}
}
Here I just want to explain. just download the source code you will get the exact source code. In Android 11 or above you just give the setting permission to read the files

how to increase listening time of speech recognition (Speech to Text) android (SpeechRecognizer)

I am working on app which require speech to text. I have integrated SpeechRecognizer service. Please check below demo project code for same. Here I have tested that SpeechRecognizer stop listening automatically after 10 to 15 seconds. So I looked for following solution to increase the listening time but did not worked.
Solution 1: SpeechRecognizer - time limit
For the above link i found the problem is text getting cut like Said "Hello , what is the serial number of your currency, It is ABC5654548585" so in first result i got Hello, What is the serial number of your currency and in second i got it is ABC5654548585".So I am unable to parse response properly.
Solution 2: I set the following parameters but did not worked.
EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS
EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS.
Solution 3: I also integrated mozilla speech to text but it is not accurate as google.
Please help me I am stucked here.Not able to proceed any more.
class MainActivity : AppCompatActivity() {
lateinit var speechRecognize: SpeechRecognizer
var editText: EditText? = null
var micButton: ImageView? = null
var speechRecognizerIntent: Intent? = null
var audioManager: AudioManager? = null
var textRecorded = ""
var isActive = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
speechRecognize = SpeechRecognizer.createSpeechRecognizer(this)
audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager?
muteRecognition(true)
editText = findViewById(R.id.text);
micButton = findViewById(R.id.button);
micButton?.setOnClickListener {
if (it.tag == null) {
isActive = true
launchSpeechIntent()
speechRecognize.startListening(speechRecognizerIntent)
it.tag = 1
} else if (it.tag == 1) {
isActive = false
parseText()
speechRecognize.stopListening()
speechRecognize.destroy()
it.tag = null
}
}
val permissions = ArrayList<String>()
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.RECORD_AUDIO
)
!= PackageManager.PERMISSION_GRANTED
) {
permissions.add(Manifest.permission.RECORD_AUDIO)
}
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
!= PackageManager.PERMISSION_GRANTED
) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
if (permissions.isNotEmpty()) {
ActivityCompat.requestPermissions(
this,
permissions.toArray(arrayOf<String>()),
10
)
} else
launchSpeechIntent()
//else
// startRequest()
}
#Suppress("DEPRECATION")
private fun muteRecognition(mute: Boolean) {
audioManager?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val flag = if (mute) AudioManager.ADJUST_MUTE else AudioManager.ADJUST_UNMUTE
it.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, flag, 0)
it.adjustStreamVolume(AudioManager.STREAM_ALARM, flag, 0)
it.adjustStreamVolume(AudioManager.STREAM_MUSIC, flag, 0)
it.adjustStreamVolume(AudioManager.STREAM_RING, flag, 0)
it.adjustStreamVolume(AudioManager.STREAM_SYSTEM, flag, 0)
} else {
it.setStreamMute(AudioManager.STREAM_NOTIFICATION, mute)
it.setStreamMute(AudioManager.STREAM_ALARM, mute)
it.setStreamMute(AudioManager.STREAM_MUSIC, mute)
it.setStreamMute(AudioManager.STREAM_RING, mute)
it.setStreamMute(AudioManager.STREAM_SYSTEM, mute)
}
}
}
fun launchSpeechIntent() {
speechRecognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent?.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
);
speechRecognizerIntent?.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.packageName)
speechRecognizerIntent?.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechRecognizerIntent?.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1)
speechRecognizerIntent?.putExtra(
RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS,
30000
)
speechRecognize.setRecognitionListener(object : RecognitionListener {
override fun onReadyForSpeech(params: Bundle?) {
Log.e("ready for speeach", "true")
}
override fun onRmsChanged(rmsdB: Float) {
Log.e("RMS changed", rmsdB.toString())
}
override fun onBufferReceived(buffer: ByteArray?) {
Log.e("buffer", buffer.toString())
}
override fun onPartialResults(partialResults: Bundle?) {
Log.e("ready for speeach", "true" + partialResults.toString())
}
override fun onEvent(eventType: Int, params: Bundle?) {
Log.e("event", eventType.toString())
Log.e("params", params.toString())
}
override fun onBeginningOfSpeech() {
editText!!.setHint("Listening...........")
editText!!.setText("")
}
override fun onEndOfSpeech() {
}
override fun onError(error: Int) {
Log.e("Error", error.toString())
speechRecognize.startListening(speechRecognizerIntent)
//editText?.setText("Error:"+error.toString())
}
override fun onResults(results: Bundle?) {
val data: ArrayList<String>? =
results!!.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
var flot = results!!.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES)
textRecorded += "#${data!!.first()}"
// restart()
}
})
}
fun restart() {
if (isActive) {
speechRecognize.destroy()
launchSpeechIntent()
speechRecognize.startListening(speechRecognizerIntent)
} else {
speechRecognize.stopListening()
speechRecognize.destroy()
}
}
override fun onResume() {
super.onResume()
}
fun parseText() {
try {
editText?.setText(textRecorded)
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.RECORD_AUDIO),
10
)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
10 ->
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
launchSpeechIntent()
}
}
}
override fun onDestroy() {
super.onDestroy()
speechRecognize.stopListening()
speechRecognize.destroy()
}
}

change the toolbar menu in android when the recycle view items are longClicked

I have implemented a recycle view and add images to the view by the camera. It is working fine. Now what i am going to do is, when the user long clicks on a recycle view item change the toolbar menu. I am getting the following error.
Process: com.example.fyp_patient, PID: 8797
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:164)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:677)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
at com.example.fyp_patient.MainActivity._$_findCachedViewById(Unknown Source:25)
at com.example.fyp_patient.MainActivity.onLongClick(ImageRecycleViewActivity.kt:237)
at android.view.View.performLongClickInternal(View.java:7444)
at android.view.View.performLongClick(View.java:7392)
at android.view.View.performLongClick(View.java:7410)
at android.view.View$CheckForLongPress.run(View.java:27811)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7188)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
My code is as follow.
ImageRecycleViewActivity.kt
var isInActionMode = false
private val uriArrayList = ArrayList<Uri>()
private val arrayList = ArrayList<Model>()
private var driveServiceHelper: DriveServiceHelper? = null
private var RC_AUTHORIZE_DRIVE = 101
private val REQUEST_IMAGE_CAPTURE: Int = 100
private var image_uri: Uri? = null
private val IMAGE_CAPTURE_CODE: Int = 101
private var ACCESS_DRIVE_SCOPE = Scope(Scopes.DRIVE_FILE)
private var SCOPE_EMAIL = Scope(Scopes.EMAIL)
lateinit var googleDriveService: Drive
var mDriveServiceHelper: DriveServiceHelper? = null
var adapter = Adapter(arrayList,this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_imagerecycleview)
setSupportActionBar(toolbar as Toolbar?)
updateView()
counter_text.visibility = View.GONE
btn.setOnClickListener {
checkForGooglePermissions()
createFolderInDrive()
}
fab.setOnClickListener {
getPermission()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.main, menu)
return true
}
/***********get the permission from the device to access the camera***********/
private fun getPermission() {
//if the system is marshmallow or above get the run time permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED ||
checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED
) {
//permission was not enabled
val permission = arrayOf(
android.Manifest.permission.CAMERA,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
)
//show popup to request permission
requestPermissions(permission, REQUEST_IMAGE_CAPTURE)
} else {
//permission already granted
openCamera()
}
} else {
//system os < marshmallow
openCamera()
}
}
/************open the device camera*************/
private fun openCamera() {
val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, "New Picture")
values.put(MediaStore.Images.Media.DESCRIPTION, "this is an images")
image_uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
//camera Intent
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri)
startActivityForResult(cameraIntent, IMAGE_CAPTURE_CODE)
}
/*************call when user clicks on the permission request dialog********************/
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
//called when user allow or deny from permission request
when (requestCode) {
REQUEST_IMAGE_CAPTURE -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission from pop up was granted
openCamera()
} else {
//permission from pop up was denied
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
}
}
}
}
/************called when an image is captured*************/
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
//called when image is captured from camera intent
if (resultCode == Activity.RESULT_OK) {
//set the image to the image view
image_uri?.let { uriArrayList.add(it) }
arrayList.add((image_uri?.let { Model("My title", "My description", it) }!!))
Log.i("check123", image_uri.toString())
updateView()
}
}
private fun updateView() {
val adapter = Adapter(arrayList, this)
recycleView.layoutManager = LinearLayoutManager(this)
recycleView.adapter = adapter
}
override fun onLongClick(view: View?): Boolean {
Log.i("clicktest","clicked")
toolbar.menu.clear()
toolbar.inflateMenu(R.menu.menu_action_mode)
counter_text.visibility=View.VISIBLE
isInActionMode=true
adapter.notifyDataSetChanged()
ActionBar.DISPLAY_HOME_AS_UP
return true
}
}
Adapter.kt
class Adapter(val arrayList: ArrayList<Model>,val context:Context) :
RecyclerView.Adapter<Adapter.ViewHolder>() {
class ViewHolder (itemView : View) : RecyclerView.ViewHolder(itemView){
fun bindItems (model: Model){
itemView.title.text = model.title
itemView.description.text = model.des
itemView.imageIv.setImageURI(model.uri)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.image_list_item, parent, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return arrayList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindItems(arrayList[position])
if (!MainActivity().isInActionMode){
holder.itemView.checkbox.visibility=View.GONE
}else {
holder.itemView.checkbox.visibility=View.VISIBLE
}
holder.itemView.setOnLongClickListener(MainActivity())
}
}
Error occurs from this line
toolbar.menu.clear()
Please help me

BLE improve permission check for ScanCallback() in Kotlin

I'm working on a small app with a BLE device. Sometimes the scan callback works fine and I can find the device but other times the scan callback doesn't work and I was wondering if there's something wrong with my code or if there's a problem with my permission check.
Here's my code:
override fun onCreate(savedInstanceState: Bundle?) {
if (!isBLESupported(this)) {
Toast.makeText(this, "This device doesn't support bluetooth", Toast.LENGTH_SHORT).show()
finish()
} else {
if (!mBtAdapter!!.isEnabled) {
val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BLUETOOTH)
}
}
adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, macList)
list_view.adapter = adapter
private val lightUUID = UUID.fromString("0000ffb0-0000-1000-8000-00805f9b34fb")
scan_button.setOnClickListener {
scanForDeviceWithFilter(lightUUID)
}
}
private fun scanForDeviceWithFilter(serviceUUID: UUID) {
val uuid = ParcelUuid(id)
val filter = ScanFilter.Builder().setServiceUuid(uuid).build()
val filters = listOf(filter)
val settings = ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build()
checkBTPermissions()
mBtAdapter!!.bluetoothLeScanner.startScan(filters, settings, scanDevicesCallback)
}
private val scanDevicesCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult?) {
result?.let {
if (!macList.contains(result.device.name.toString())) {
macList.add(result.device.name.toString())
adapter.notifyDataSetChanged()
}
Log.d(TAG, "device found:${result.device}")
}
}
override fun onScanFailed(errorCode: Int) {
Log.d(TAG, "Scan failed $errorCode")
}
}
private fun isBLESupported(context: Context): Boolean {
return BluetoothAdapter.getDefaultAdapter() != null && context.packageManager.hasSystemFeature(
PackageManager.FEATURE_BLUETOOTH_LE
)
}
private fun checkBTPermissions() {
val permissionCheck = checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION")
if (permissionCheck != 0) {
requestPermissions(
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1001
)
}
}
init {
mBtAdapter = BluetoothAdapter.getDefaultAdapter()
}

Categories

Resources