I implemented Scope Storage in a sample project. Where I Save, Load, and Modify Images in Local Storage and in Scope Storage as well.
Below is my Main Activity Class where I am Saving, Loading, and Modifying images in Local and Scope Storage. The below Code was working in API level 28 or Android 10. but when I run this app in Android 11 it gets hang and gives Application Not Responding. I am unable to find any error in Logcat. Below is my code Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.scopestorage">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.ScopeStorage">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now Below is my Main Activity Code:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var internalStoragePhotoAdapter: InternalStoragePhotoAdapter
private lateinit var externalStoragePhotoAdapter: SharedPhotoAdapter
private var readPermissionGranted = false
private var writePermissionGranted = false
private lateinit var permissionsLauncher: ActivityResultLauncher<Array<String>>
private lateinit var intentSenderLauncher: ActivityResultLauncher<IntentSenderRequest>
private lateinit var contentObserver: ContentObserver
private var deletedImageUri: Uri? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
internalStoragePhotoAdapter = InternalStoragePhotoAdapter {
lifecycleScope.launch {
val isDeletionSuccessful = deletePhotoFromInternalStorage(it.name)
if(isDeletionSuccessful) {
loadPhotosFromInternalStorageIntoRecyclerView()
Toast.makeText(this#MainActivity, "Photo successfully deleted", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this#MainActivity, "Failed to delete photo", Toast.LENGTH_SHORT).show()
}
}
}
externalStoragePhotoAdapter = SharedPhotoAdapter {
lifecycleScope.launch {
deletePhotoFromExternalStorage(it.contentUri)
deletedImageUri = it.contentUri
}
}
setupExternalStorageRecyclerView()
initContentObserver()
permissionsLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
readPermissionGranted = permissions[Manifest.permission.READ_EXTERNAL_STORAGE] ?: readPermissionGranted
writePermissionGranted = permissions[Manifest.permission.WRITE_EXTERNAL_STORAGE] ?: writePermissionGranted
if(readPermissionGranted) {
loadPhotosFromExternalStorageIntoRecyclerView()
} else {
Toast.makeText(this, "Can't read files without permission.", Toast.LENGTH_LONG).show()
}
}
updateOrRequestPermissions()
intentSenderLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
if(it.resultCode == RESULT_OK) {
if(Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
lifecycleScope.launch {
deletePhotoFromExternalStorage(deletedImageUri ?: return#launch)
}
}
Toast.makeText(this#MainActivity, "Photo deleted successfully", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this#MainActivity, "Photo couldn't be deleted", Toast.LENGTH_SHORT).show()
}
}
val takePhoto = registerForActivityResult(ActivityResultContracts.TakePicturePreview()) {
lifecycleScope.launch {
val isPrivate = binding.switchPrivate.isChecked
val isSavedSuccessfully = when {
isPrivate -> savePhotoToInternalStorage(UUID.randomUUID().toString(), it)
writePermissionGranted -> savePhotoToExternalStorage(UUID.randomUUID().toString(), it)
else -> false
}
if(isPrivate) {
loadPhotosFromInternalStorageIntoRecyclerView()
}
if(isSavedSuccessfully) {
Toast.makeText(this#MainActivity, "Photo saved successfully", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this#MainActivity, "Failed to save photo", Toast.LENGTH_SHORT).show()
}
}
}
binding.btnTakePhoto.setOnClickListener {
takePhoto.launch()
}
setupInternalStorageRecyclerView()
loadPhotosFromInternalStorageIntoRecyclerView()
loadPhotosFromExternalStorageIntoRecyclerView()
}
private fun initContentObserver() {
contentObserver = object : ContentObserver(null) {
override fun onChange(selfChange: Boolean) {
if(readPermissionGranted) {
loadPhotosFromExternalStorageIntoRecyclerView()
}
}
}
contentResolver.registerContentObserver(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
true,
contentObserver
)
}
private suspend fun deletePhotoFromExternalStorage(photoUri: Uri) {
withContext(Dispatchers.IO) {
try {
contentResolver.delete(photoUri, null, null)
} catch (e: SecurityException) {
val intentSender = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
MediaStore.createDeleteRequest(contentResolver, listOf(photoUri)).intentSender
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> {
val recoverableSecurityException = e as? RecoverableSecurityException
recoverableSecurityException?.userAction?.actionIntent?.intentSender
}
else -> null
}
intentSender?.let { sender ->
intentSenderLauncher.launch(
IntentSenderRequest.Builder(sender).build()
)
}
}
}
}
private suspend fun loadPhotosFromExternalStorage(): List<SharedStoragePhoto> {
return withContext(Dispatchers.IO) {
val collection = sdk29AndUp {
MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
} ?: MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf(
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.WIDTH,
MediaStore.Images.Media.HEIGHT,
)
val photos = mutableListOf<SharedStoragePhoto>()
contentResolver.query(
collection,
projection,
null,
null,
"${MediaStore.Images.Media.DISPLAY_NAME} ASC"
)?.use { cursor ->
val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
val displayNameColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)
val widthColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.WIDTH)
val heightColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.HEIGHT)
while(cursor.moveToNext()) {
val id = cursor.getLong(idColumn)
val displayName = cursor.getString(displayNameColumn)
val width = cursor.getInt(widthColumn)
val height = cursor.getInt(heightColumn)
val contentUri = ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
id
)
photos.add(SharedStoragePhoto(id, displayName, width, height, contentUri))
}
photos.toList()
} ?: listOf()
}
}
private fun updateOrRequestPermissions() {
val hasReadPermission = ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
val hasWritePermission = ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
val minSdk29 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
readPermissionGranted = hasReadPermission
writePermissionGranted = hasWritePermission || minSdk29
val permissionsToRequest = mutableListOf<String>()
if(!writePermissionGranted) {
permissionsToRequest.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
if(!readPermissionGranted) {
permissionsToRequest.add(Manifest.permission.READ_EXTERNAL_STORAGE)
}
if(permissionsToRequest.isNotEmpty()) {
permissionsLauncher.launch(permissionsToRequest.toTypedArray())
}
}
private suspend fun savePhotoToExternalStorage(displayName: String, bmp: Bitmap): Boolean {
return withContext(Dispatchers.IO) {
val imageCollection = sdk29AndUp {
MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
} ?: MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val contentValues = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, "$displayName.jpg")
put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
put(MediaStore.Images.Media.WIDTH, bmp.width)
put(MediaStore.Images.Media.HEIGHT, bmp.height)
}
try {
contentResolver.insert(imageCollection, contentValues)?.also { uri ->
contentResolver.openOutputStream(uri).use { outputStream ->
if(!bmp.compress(Bitmap.CompressFormat.JPEG, 95, outputStream)) {
throw IOException("Couldn't save bitmap")
}
}
} ?: throw IOException("Couldn't create MediaStore entry")
true
} catch(e: IOException) {
e.printStackTrace()
false
}
}
}
private fun setupInternalStorageRecyclerView() = binding.rvPrivatePhotos.apply {
adapter = internalStoragePhotoAdapter
layoutManager = StaggeredGridLayoutManager(3, RecyclerView.VERTICAL)
}
private fun setupExternalStorageRecyclerView() = binding.rvPublicPhotos.apply {
adapter = externalStoragePhotoAdapter
layoutManager = StaggeredGridLayoutManager(3, RecyclerView.VERTICAL)
}
private fun loadPhotosFromInternalStorageIntoRecyclerView() {
lifecycleScope.launch {
val photos = loadPhotosFromInternalStorage()
internalStoragePhotoAdapter.submitList(photos)
}
}
private fun loadPhotosFromExternalStorageIntoRecyclerView() {
lifecycleScope.launch {
val photos = loadPhotosFromExternalStorage()
externalStoragePhotoAdapter.submitList(photos)
}
}
private suspend fun deletePhotoFromInternalStorage(filename: String): Boolean {
return withContext(Dispatchers.IO) {
try {
deleteFile(filename)
} catch (e: Exception) {
e.printStackTrace()
false
}
}
}
private suspend fun loadPhotosFromInternalStorage(): List<InternalStoragePhoto> {
return withContext(Dispatchers.IO) {
val files = filesDir.listFiles()
files?.filter { it.canRead() && it.isFile && it.name.endsWith(".jpg") }?.map {
val bytes = it.readBytes()
val bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
InternalStoragePhoto(it.name, bmp)
} ?: listOf()
}
}
private suspend fun savePhotoToInternalStorage(filename: String, bmp: Bitmap): Boolean {
return withContext(Dispatchers.IO) {
try {
openFileOutput("$filename.jpg", MODE_PRIVATE).use { stream ->
if(!bmp.compress(Bitmap.CompressFormat.JPEG, 95, stream)) {
throw IOException("Couldn't save bitmap.")
}
}
true
} catch(e: IOException) {
e.printStackTrace()
false
}
}
}
override fun onDestroy() {
super.onDestroy()
contentResolver.unregisterContentObserver(contentObserver)
}
}
Any sort of help would be highly appreciated.
Related
I use camera2 for capture video inside the app.
It works fine from api 26 and above, but it is now working in api 25 and 24.
the preview of video before starting video capture is ok, but after i click the record video, it crashed.
My code:
private var PERMISSIONS_REQUIRED = arrayOf(
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO
)
class MainActivity : AppCompatActivity() {
private lateinit var activityMainBinding: ActivityMainBinding
private val outputDirectory: String by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
//TODO("Change save folder name")
"${Environment.DIRECTORY_DCIM}/InAppCamera/"
} else {
//TODO("Change save folder name")
"${getExternalFilesDir(Environment.DIRECTORY_DCIM)}/InAppCamera/"
}
}
private var camera: Camera? = null
private var cameraProvider: ProcessCameraProvider? = null
private var preview: Preview? = null
private var videoCapture: VideoCapture? = null
private var lensFacing = CameraSelector.DEFAULT_FRONT_CAMERA
private var isRecording = false
private val animateRecord by lazy {
ObjectAnimator.ofFloat(activityMainBinding.btnRecordVideo, View.ALPHA, 1f, 0.5f).apply {
repeatMode = ObjectAnimator.REVERSE
repeatCount = ObjectAnimator.INFINITE
doOnCancel { activityMainBinding.btnRecordVideo.alpha = 1f }
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(activityMainBinding.root)
getPermission()
activityMainBinding.btnRecordVideo.setOnClickListener {
recordVideo()
}
activityMainBinding.btnSwitchCamera.setOnClickListener { toggleCamera() }
activityMainBinding.btnRemoveVideo.setOnClickListener { removeVideo() }
}
#SuppressLint("RestrictedApi")
private fun startCamera() {
val viewFinder = activityMainBinding.viewFinder
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
cameraProvider = cameraProviderFuture.get()
val rotation = viewFinder.display.rotation
val localCameraProvider = cameraProvider
?: throw IllegalStateException("Camera initialization failed.")
preview = Preview.Builder()
.setTargetAspectRatio(AspectRatio.RATIO_4_3)
.setTargetRotation(rotation)
.build()
//TODO("Change size of video")
val size = Size(400, 400)
//TODO("Change setVideoFrameRate and setAudioBitRate")
val videoCaptureConfig =
VideoCapture.DEFAULT_CONFIG.config
videoCapture = VideoCapture.Builder
.fromConfig(videoCaptureConfig)
.setVideoFrameRate(10)
.setMaxResolution(size)
.build()
localCameraProvider.unbindAll()
try {
camera = localCameraProvider.bindToLifecycle(
this#MainActivity,
lensFacing,
preview,
videoCapture,
)
preview?.setSurfaceProvider(viewFinder.surfaceProvider)
} catch (e: Exception) {
Log.e(TAG, "Failed to bind use cases", e)
}
}, ContextCompat.getMainExecutor(this))
}
#SuppressLint("RestrictedApi")
private fun recordVideo() {
val localVideoCapture =
videoCapture ?: throw IllegalStateException("Camera initialization failed.")
val outputOptions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, System.currentTimeMillis())
put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4")
put(MediaStore.MediaColumns.RELATIVE_PATH, outputDirectory)
}
contentResolver.run {
val contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
VideoCapture.OutputFileOptions.Builder(this, contentUri, contentValues)
}
} else {
File(outputDirectory).mkdirs()
val file = File("$outputDirectory/${System.currentTimeMillis()}.mp4")
VideoCapture.OutputFileOptions.Builder(file)
}.build()
if (!isRecording) {
if (activityMainBinding.videoView.isPlaying) {
activityMainBinding.btnRemoveVideo.visibility = View.GONE
activityMainBinding.videoView.pause()
activityMainBinding.videoView.visibility = View.GONE
}
animateRecord.start()
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.RECORD_AUDIO
) != PackageManager.PERMISSION_GRANTED
) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return
}
val timestamp = System.currentTimeMillis()
val contentValues = ContentValues()
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, timestamp)
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4")
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
localVideoCapture.startRecording(
VideoCapture.OutputFileOptions.Builder(
contentResolver,
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
contentValues
).build(),
mainExecutor(),
object : VideoCapture.OnVideoSavedCallback {
override fun onVideoSaved(outputFileResults: VideoCapture.OutputFileResults) {
Toast.makeText(
this#MainActivity,
"Video has been saved successfully.",
Toast.LENGTH_SHORT
).show()
activityMainBinding.btnRemoveVideo.visibility = View.VISIBLE
playVideo(outputFileResults.savedUri!!)
}
override fun onError(
videoCaptureError: Int,
message: String,
cause: Throwable?
) {
animateRecord.cancel()
Log.e("onCaptureError", message)
Toast.makeText(
this#MainActivity,
"Error saving video: $message",
Toast.LENGTH_SHORT
).show()
Toast.makeText(
this#MainActivity,
"Ino baram aks begir: ${cause!!.message}",
Toast.LENGTH_LONG
).show()
}
}
)
} else {
localVideoCapture.startRecording(
outputOptions,
mainExecutor(),
object : VideoCapture.OnVideoSavedCallback {
override fun onVideoSaved(outputFileResults: VideoCapture.OutputFileResults) {
// Create small preview
outputFileResults.savedUri
?.let { uri ->
activityMainBinding.btnRemoveVideo.visibility = View.VISIBLE
playVideo(uri)
Log.d(TAG, "Video saved in $uri")
}
}
override fun onError(
videoCaptureError: Int,
message: String,
cause: Throwable?
) {
Log.e("onCaptureError", message)
// This function is called if there is an error during recording process
animateRecord.cancel()
val msg = "Video capture failed: $message"
Toast.makeText(this#MainActivity, msg, Toast.LENGTH_SHORT).show()
Log.e(TAG, msg)
cause?.printStackTrace()
}
})
}
} else {
animateRecord.cancel()
localVideoCapture.stopRecording()
}
isRecording = !isRecording
}
private fun playVideo(uri: Uri) {
activityMainBinding.videoView.visibility = View.VISIBLE
activityMainBinding.videoView.setVideoURI(uri)
activityMainBinding.videoView.requestFocus()
activityMainBinding.videoView.start()
pauseVideo()
}
private var videoPosition = 0
private fun pauseVideo() {
activityMainBinding.videoView.setOnClickListener {
if (activityMainBinding.videoView.isPlaying) {
videoPosition = activityMainBinding.videoView.currentPosition
activityMainBinding.videoView.pause()
} else {
activityMainBinding.videoView.seekTo(videoPosition)
activityMainBinding.videoView.start()
}
}
}
private fun removeVideo() {
activityMainBinding.videoView.visibility = View.GONE
activityMainBinding.btnRemoveVideo.visibility = View.GONE
startCamera()
}
private fun toggleCamera() = activityMainBinding.btnSwitchCamera.toggleButton(
flag = lensFacing == CameraSelector.DEFAULT_FRONT_CAMERA,
rotationAngle = 180f,
firstIcon = R.drawable.ic_outline_camera_rear,
secondIcon = R.drawable.ic_outline_camera_front,
) {
lensFacing = if (it) {
CameraSelector.DEFAULT_FRONT_CAMERA
} else {
CameraSelector.DEFAULT_BACK_CAMERA
}
startCamera()
}
private fun ImageButton.toggleButton(
flag: Boolean,
rotationAngle: Float,
#DrawableRes firstIcon: Int,
#DrawableRes secondIcon: Int,
action: (Boolean) -> Unit
) {
if (flag) {
if (rotationY == 0f) rotationY = rotationAngle
animate().rotationY(0f).apply {
setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator?) {
super.onAnimationEnd(animation)
action(!flag)
}
})
}.duration = 200
GlobalScope.launch(Dispatchers.Main) {
delay(100)
setImageResource(firstIcon)
}
} else {
if (rotationY == rotationAngle) rotationY = 0f
animate().rotationY(rotationAngle).apply {
setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator?) {
super.onAnimationEnd(animation)
action(!flag)
}
})
}.duration = 200
GlobalScope.launch(Dispatchers.Main) {
delay(100)
setImageResource(secondIcon)
}
}
}
private fun getPermission() {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
val permissionList = PERMISSIONS_REQUIRED.toMutableList()
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
PERMISSIONS_REQUIRED = permissionList.toTypedArray()
}
if (!hasPermissions(this)) {
activityResultLauncher.launch(PERMISSIONS_REQUIRED)
activityMainBinding.btnRecordVideo.visibility = View.GONE
activityMainBinding.btnSwitchCamera.visibility = View.GONE
} else {
activityMainBinding.btnRecordVideo.visibility = View.VISIBLE
activityMainBinding.btnSwitchCamera.visibility = View.VISIBLE
startCamera()
}
}
private fun Context.mainExecutor(): Executor =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
mainExecutor
} else {
MainExecutor()
}
companion object {
fun hasPermissions(context: Context) = PERMISSIONS_REQUIRED.all {
ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}
}
private val activityResultLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions())
{ permissions ->
// Handle Permission granted/rejected
var permissionGranted = true
permissions.entries.forEach {
if (it.key in PERMISSIONS_REQUIRED && !it.value)
permissionGranted = false
}
if (!permissionGranted) {
activityMainBinding.btnRecordVideo.visibility = View.GONE
activityMainBinding.btnSwitchCamera.visibility = View.GONE
Toast.makeText(this, "Permission request denied", Toast.LENGTH_LONG).show()
} else {
activityMainBinding.btnRecordVideo.visibility = View.VISIBLE
activityMainBinding.btnSwitchCamera.visibility = View.VISIBLE
startCamera()
}
}
}
this is the crash log:
E/ACodec: OMX/mediaserver died, signalling error!
E/ACodec: signalError(omxError 0x8000100d, internalError -32)
E/ACodec: OMX/mediaserver died, signalling error!
E/ACodec: signalError(omxError 0x8000100d, internalError -32)
E/MediaCodec: Codec reported err 0xffffffe0, actionCode 0, while in state 6
E/MediaCodec: Codec reported err 0xffffffe0, actionCode 0, while in state 6
E/MediaCodec: getBufferAndFormat - not executing
I/VideoCapture: audio dequeueInputBuffer IllegalStateException null
E/AndroidRuntime: FATAL EXCEPTION: CameraX-video encoding thread
Process: ir.arinateam.inappcamera, PID: 3050
java.lang.IllegalStateException
at android.media.MediaCodec.native_dequeueOutputBuffer(Native Method)
at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:2570)
at androidx.camera.core.VideoCapture.videoEncode(VideoCapture.java:878)
at androidx.camera.core.VideoCapture.lambda$startRecording$4$androidx-camera-core-VideoCapture(VideoCapture.java:508)
at androidx.camera.core.VideoCapture$$ExternalSyntheticLambda6.run(D8$$SyntheticClass)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)
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
I need to fetch data inside WhatsApp folders on External Storage.
As i am targeting API Level 30 i am no longer able to access WhatsApp folders on External Storage. I have implemented Storage Access Framework and got Android/media folder Uri and Document File. And using listFiles() i am able to list files but with filter() and sortedByDescending() functions it becomes very slow.
What i have tried?
Used Cursor loader with Projection and Selection Arguments but it only
worked for non hidden folders like WhatsApp Images and WhatsApp Videos
It returns empty cursor for hidden folder .Statuses
Tried replacing MediaStore.Video.Media.EXTERNAL_CONTENT_URI with MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
What is required?
List images and videos from .Statuses folder same as i am listing WhatsApp Images using Media Store in HomeActivity.java
Below is my code
In this activity i get permision to Android/media and set all WhatsApp folders URIs for status fetching and other use, but fetched WhatsApp Images with projection and selection from WhatsApp Images folder
class HomeActivity : AppCompatActivity(), InternetListener, PurchasesUpdatedListener,
CoroutineScope {
private val exceptionHandler = CoroutineExceptionHandler { context, exception ->
Toast.makeText(this, exception.message, Toast.LENGTH_LONG).show()
}
private val dataRepository: DataRepository by inject()
val tinyDB: TinyDB by inject()
val REQUEST_CODE = 12123
init {
newNativeAdSetUp = null
}
val sharedViewModel by viewModel<SharedViewModel>()
val viewModel by viewModel<HomeViewModel>()
val handler = CoroutineExceptionHandler { _, exception ->
Log.d("CoroutineException", "$exception handled !")
}
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job + handler
private lateinit var job: Job
val sdk30PermissionListener = object : PermissionListener {
override fun onPermissionGranted() {
openDocumentTree()
}
override fun onPermissionDenied(deniedPermissions: MutableList<String>?) {
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
handlePermissionsByVersion()
}
private fun handlePermissionsByVersion() {
if (SDK_INT >= Build.VERSION_CODES.R) {
if ((ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
== PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
)
== PackageManager.PERMISSION_GRANTED)
) {
//if granted load whatsapp images and some uris setup to viewmodel
loadWhatsAppImages()
if (arePermissionsGranted()) {
if (dataRepository.mrWhatsAppImages == null || dataRepository.mrWhatsAppBusinessImages == null) {
setUpWAURIs()
}
}
} else {
TedPermission.with(this)
.setPermissionListener(sdk30PermissionListener)
.setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
.check()
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, #Nullable data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
if (data != null) {
//this is the uri user has provided us
val treeUri: Uri? = data.data
if (treeUri != null) {
sharedViewModel.treeUri = treeUri
val decoded = Uri.decode(treeUri.toString())
Log.i(LOGTAG, "got uri: ${treeUri.toString()}")
// here we should do some checks on the uri, we do not want root uri
// because it will not work on Android 11, or perhaps we have some specific
// folder name that we want, etc
if (Uri.decode(treeUri.toString()).endsWith(":")) {
showWrongFolderSelection()
return
}
if (!decoded.equals(Constants.WHATSAPP_MEDIA_URI_DECODED)) {
showWrongFolderSelection()
return
}
// here we ask the content resolver to persist the permission for us
val takeFlags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver.takePersistableUriPermission(
treeUri,
takeFlags
)
val treeUriAsString = treeUri.toString()
tinyDB.putString("FOLDER_URI", treeUriAsString)
if (SDK_INT >= Build.VERSION_CODES.R) {
setupPaths()
}
}
}
}
}
private fun setupPaths() {
setUpOverlay()
fetchWhatsAppRootURIs(
this,
sharedViewModel,
dataRepository,
tinyDB
) {
fetchWhatsAppBusinessRootURIs(
this,
sharedViewModel,
dataRepository,
tinyDB
) {
tinyDB.putBoolean("WARootPathsDone", true)
removeOverlay()
}
}
}
override fun onDestroy() {
dialogHandler.removeCallbacksAndMessages(null)
super.onDestroy()
}
val loadmanagerImages = object : LoaderManager.LoaderCallbacks<Cursor> {
val whatsAppImagesArrayList = arrayListOf<File>()
override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> {
var location: File = File(
Environment.getExternalStorageDirectory()
.toString() + Constants.whatsapp_images_path
)
if (!location.exists()) {
location = File(
Environment.getExternalStorageDirectory()
.toString() + Constants.whatsapp_images_path11
)
}
if (location != null && location.exists()) {
whatsAppImagesArrayList.clear()
Timber.e("checkLoaded-onCreateLoader $id")
if (id == 0) {
var folder = location.absolutePath
val projection = arrayOf(
MediaStore.MediaColumns.DATA,
MediaStore.MediaColumns.DATE_MODIFIED
)
val selection = MediaStore.Images.Media.DATA + " like ? "
val selectionArgs: String = "%$folder%"
return CursorLoader(
this#HomeActivity,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
arrayOf(selectionArgs),
"${MediaStore.Images.Media.DATE_MODIFIED} DESC"
)
}
}
return null!!
}
override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor?) {
Timber.e("checkLoaded-onLoadFinished")
var absolutePathOfImage: String
if (loader.id == 0) {
cursor?.let {
val columnIndexData = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)
GlobalScope.launch(Dispatchers.Main + exceptionHandler) {
async(Dispatchers.IO + exceptionHandler) {
while (!cursor.isClosed && cursor.moveToNext() == true) {
absolutePathOfImage = cursor.getString(columnIndexData!!)
whatsAppImagesArrayList.add(File(absolutePathOfImage))
}
}.await()
LoaderManager.getInstance(this#HomeActivity).destroyLoader(0)
Timber.e("checkLoaded-Completion")
galleryViewModel.whatsAppImagesList.postValue(whatsAppImagesArrayList)
}
}
}
}
override fun onLoaderReset(loader: Loader<Cursor>) {
}
}
fun loadWhatsAppImages() {
try {
tinyDB.putBoolean("whatsAppMediaLoadCalled", true)
LoaderManager.getInstance(this).initLoader(
0,
null,
loadmanagerImages
)
} catch (e: RuntimeException) {
Log.e("exVideos ", "ex : ${e.localizedMessage}")
}
}
companion object {
const val ANDROID_DOCID = "primary:Android/media/"
const val EXTERNAL_STORAGE_PROVIDER_AUTHORITY = "com.android.externalstorage.documents"
private val androidUri = DocumentsContract.buildDocumentUri(
EXTERNAL_STORAGE_PROVIDER_AUTHORITY, ANDROID_DOCID
)
val androidTreeUri = DocumentsContract.buildTreeDocumentUri(
EXTERNAL_STORAGE_PROVIDER_AUTHORITY, ANDROID_DOCID
)
}
private fun openDocumentTree() {
val uriString = tinyDB.getString("FOLDER_URI", "")
when {
uriString == "" -> {
Log.w(LOGTAG, "uri not stored")
askPermission()
}
arePermissionsGranted() -> {
}
else -> {
Log.w(LOGTAG, "uri permission not stored")
askPermission()
}
}
}
// this will present the user with folder browser to select a folder for our data
private fun askPermission() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, androidUri)
startActivityForResult(intent, REQUEST_CODE)
}
private fun arePermissionsGranted(): Boolean {
var uriString = tinyDB.getString("FOLDER_URI", "")
val list = contentResolver.persistedUriPermissions
for (i in list.indices) {
val persistedUriString = list[i].uri.toString()
if (persistedUriString == uriString && list[i].isWritePermission && list[i].isReadPermission) {
return true
}
}
return false
}
private fun showWrongFolderSelection() {
val layoutInflaterAndroid = LayoutInflater.from(this)
val mView = layoutInflaterAndroid.inflate(R.layout.layout_dialog_wrong_folder, null)
val builder = AlertDialog.Builder(this, R.style.ThemePageSearchDialog)
builder.setView(mView)
val alertDialog = builder.show()
alertDialog.setCancelable(false)
val btnOk = mView.findViewById(R.id.tvExit) as TextView
val tvCancel = mView.findViewById(R.id.tvCancel) as TextView
btnOk.setOnClickListener {
alertDialog.dismiss()
openDocumentTree()
}
tvCancel.setOnClickListener {
alertDialog.dismiss()
}
}
private fun setUpWAURIs() {
dataRepository.mrWhatsAppImages =
getDocumentFileFromStringURI(
this,
tinyDB.getString("mrWhatsAppImages")
)
dataRepository.mrWhatsAppVN =
getDocumentFileFromStringURI(
this,
tinyDB.getString("mrWhatsAppVN")
)
dataRepository.mrWhatsAppDocs =
getDocumentFileFromStringURI(
this,
tinyDB.getString("mrWhatsAppDocs")
)
dataRepository.mrWhatsAppVideo =
getDocumentFileFromStringURI(
this,
tinyDB.getString("mrWhatsAppVideo")
)
dataRepository.mrWhatsAppAudio =
getDocumentFileFromStringURI(
this,
tinyDB.getString("mrWhatsAppAudio")
)
dataRepository.WhatsAppStatuses =
getDocumentFileFromStringURIStatuses(
this,
tinyDB.getString("WhatsAppStatuses")
)
dataRepository.mrWhatsAppBusinessImages =
getDocumentFileFromStringURI(
this,
tinyDB.getString("mrWhatsAppBusinessImages")
)
dataRepository.mrWhatsAppBusinessVN =
getDocumentFileFromStringURI(
this,
tinyDB.getString("mrWhatsAppBusinessVN")
)
dataRepository.mrWhatsAppBusinessDocs =
getDocumentFileFromStringURI(
this,
tinyDB.getString("mrWhatsAppBusinessDocs")
)
dataRepository.mrWhatsAppBusinessVideo =
getDocumentFileFromStringURI(
this,
tinyDB.getString("mrWhatsAppBusinessVideo")
)
dataRepository.mrWhatsAppBusinessAudio =
getDocumentFileFromStringURI(
this,
tinyDB.getString("mrWhatsAppBusinessAudio")
)
dataRepository.WhatsAppBusinessStatuses =
getDocumentFileFromStringURIStatuses(
this,
tinyDB.getString("WhatsAppBusinessStatuses")
)
}
fun setUpOverlay() {
val dialogfragment = FullScreenLoadingDialog()
dialogfragment.isCancelable = false
dialogfragment.setisAdmobAd(true)
val ft: FragmentTransaction =
supportFragmentManager.beginTransaction()
ft.add(dialogfragment, "DialogFragment_FLAG")
ft.commitAllowingStateLoss()
}
fun removeOverlay() {
val fragment: Fragment? = supportFragmentManager.findFragmentByTag("DialogFragment_FLAG")
if (fragment != null && fragment is DialogFragment) {
fragment.dismissAllowingStateLoss()
}
}
fun fetchWhatsAppRootURIs(
context: Context,
sharedViewModel: SharedViewModel,
dataRepository: DataRepository,
tinyDB: TinyDB, completed: () -> Unit
) {
val selectedPackageName = Constants.WHATSAPP_PKG_NAME
val selectedRootName = Constants.WHATSAPP_ROOT_NAME
var waImages: DocumentFile? = null
var waVN: DocumentFile? = null
var waDocs: DocumentFile? = null
var waVideos: DocumentFile? = null
var waAudio: DocumentFile? = null
var waStatus: DocumentFile? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && sharedViewModel.treeUri != null) {
CoroutineScope(Dispatchers.Main).launch {
async(Dispatchers.IO) {
val dir = DocumentFile.fromTreeUri(
context,
sharedViewModel.treeUri!!
)
dir?.listFiles()?.forEach {
if (it.name.equals(selectedPackageName)) {
it.listFiles().forEach {
if (it.name.equals(selectedRootName)) {
it.listFiles().forEach {
if (it.name.equals(Constants.WHATSAPP_MEDIA_FOLDER_NAME)) {
it.listFiles().forEach {
if (it.name.equals(Constants.FOLDER_NAME_WHATSAPP_IMAGES)) {
waImages = it
} else if (it.name.equals(Constants.FOLDER_NAME_WHATSAPP_VN)) {
waVN = it
} else if (it.name.equals(Constants.FOLDER_NAME_WHATSAPP_DOCUMENTS)) {
waDocs = it
} else if (it.name.equals(Constants.FOLDER_NAME_WHATSAPP_VIDEO)) {
waVideos = it
} else if (it.name.equals(Constants.FOLDER_NAME_WHATSAPP_AUDIO)) {
waAudio = it
} else if (it.name.equals(Constants.FOLDER_NAME_STATUSES)) {
waStatus = it
}
}
}
}
}
}
}
}
}.await()
Timber.e("processStatusFetch:Done")
tinyDB.putString("mrWhatsAppImages", waImages?.uri.toString())
tinyDB.putString("mrWhatsAppVN", waImages?.uri.toString())
tinyDB.putString("mrWhatsAppDocs", waImages?.uri.toString())
tinyDB.putString("mrWhatsAppVideo", waImages?.uri.toString())
tinyDB.putString("mrWhatsAppAudio", waImages?.uri.toString())
tinyDB.putString("WhatsAppStatuses", waStatus?.uri.toString())
dataRepository.mrWhatsAppImages = waImages
dataRepository.mrWhatsAppVN = waVN
dataRepository.mrWhatsAppDocs = waDocs
dataRepository.mrWhatsAppVideo = waVideos
dataRepository.mrWhatsAppAudio = waAudio
dataRepository.WhatsAppStatuses = waStatus
completed()
}
}
}
Here i am using .Statuses folder URI to list DocumentFiles and display but this way it is slow
class StatusImageFragment : Fragment(), StatusListener, CoroutineScope {
companion object {
fun newInstance() = StatusImageFragment()
}
val handler = CoroutineExceptionHandler { _, exception ->
Log.d("CoroutineException", "$exception handled !")
}
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job + handler
private lateinit var job: Job
private var adapterSDK30 = StatusImageAdapterSDK30()
private var no_image: ImageView? = null
private var no_image_txt: TextView? = null
val tinyDB: TinyDB by inject()
val sharedViewModel by viewModel<SharedViewModel>()
private val dataRepository: DataRepository by inject()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
job = Job()
return inflater.inflate(R.layout.status_image_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
swipeRefresh(false, false)
}
public fun swipeRefresh(isReloadRequired: Boolean, isFromModeChanged: Boolean) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (isFromModeChanged) {
status_image_recycler.visibility = View.GONE
progressbar.visibility = View.VISIBLE
no_image?.let {
it.visibility = View.GONE
}
no_image_txt?.let {
it.visibility = View.GONE
}
go_to_app?.let {
it.visibility = View.GONE
}
} else {
if (adapterSDK30.listImages == null || adapterSDK30.listImages.size == 0) {
no_image?.let {
it.visibility = View.GONE
}
no_image_txt?.let {
it.visibility = View.GONE
}
go_to_app?.let {
it.visibility = View.GONE
}
progressbar.visibility = View.VISIBLE
}
}
if (isReloadRequired) {
processStatusFetchFromChild({
sharedViewModel.statusImages.observe(viewLifecycleOwner, Observer {
val arrayList = it
adapterSDK30.listImages = arrayList
postFetchingExecutionSDK30()
})
})
} else {
sharedViewModel.statusImages.observe(viewLifecycleOwner, Observer {
val arrayList = it
adapterSDK30.listImages = arrayList
adapterSDK30.listImages = it
postFetchingExecutionSDK30()
})
}
}
} catch (ex: Exception) {
ex.printStackTrace()
}
}
private fun postFetchingExecutionSDK30() {
progressbar.visibility = View.GONE
status_image_recycler.visibility = View.VISIBLE
if (adapterSDK30!!.listImages != null && adapterSDK30!!.listImages.size > 0) {
no_image?.let {
it.visibility = View.GONE
}
no_image_txt?.let {
it.visibility = View.GONE
}
go_to_app?.let {
it.visibility = View.GONE
}
} else {
no_image?.let {
it.visibility = View.VISIBLE
}
no_image_txt?.let {
it.visibility = View.VISIBLE
}
go_to_app?.let {
it.visibility = View.VISIBLE
}
}
adapterSDK30!!.notifyDataSetChanged()
status_img_swipe.isRefreshing = false
}
override fun onDestroyView() {
job.cancel()
super.onDestroyView()
}
fun processStatusFetchFromChild(completed: () -> Unit) {
val statusSelection = tinyDB.getInt(Constants.status_accounts)
if (statusSelection == 0 || statusSelection == 1) {
if (dataRepository.WhatsAppStatuses == null) {
(activity as StatusActivity).setUpWAURIs()
}
var documentFileStatuses: DocumentFile? = dataRepository.WhatsAppStatuses
if (statusSelection == 1) {
documentFileStatuses = dataRepository.WhatsAppBusinessStatuses
}
if (documentFileStatuses != null) {
launch(Dispatchers.Main) {
val statusImages1 = arrayListOf<DocumentFile>()
async(Dispatchers.IO) {
//this takes time ; want to fetch this same as WhatsApp Gallery
statusImages1.addAll(documentFileStatuses!!.listFiles().filter {
it.mimeType.equals(Constants.MIME_TYPE_IMG_PNG) || it.mimeType.equals(
Constants.MIME_TYPE_IMG_JPG
) || it.mimeType.equals(Constants.MIME_TYPE_IMG_JPEG)
}.sortedByDescending { it.lastModified() })
}.await()
Timber.e("processStatusFetch:Done")
sharedViewModel.statusImages.postValue(statusImages1)
completed()
}
} else {
Timber.e("processStatusFetch:Done")
sharedViewModel.statusImages.postValue(arrayListOf<DocumentFile>())
completed()
}
} else {
Timber.e("processStatusFetch:Done")
sharedViewModel.statusImages.postValue(arrayListOf<DocumentFile>())
completed()
}
}
}
Please note WhatsApp folder path which i used is
val whatsapp_images_path11 = "/Android/media/“ +"com.whatsapp" +"/WhatsApp/Media/WhatsAppImages/"
How i can use MediaStore in this case so that i don't need to use sort and filter functions of list? Its not important to get java.io File only i can work with URIs as well.
What I have finally implemented, in android 10+ you need to ask the user for your specific directory access. Then you can use this functions to fetch statuses:
#RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun readSDKFrom30(): ArrayList<String> {
val treeUri = DocumentsContract.buildTreeDocumentUri(
EXTERNAL_STORAGE_PROVIDER_AUTHORITY,
"primary:Android/media/com.whatsapp/WhatsApp/Media/.Statuses"
)
val tree = DocumentFile.fromTreeUri(context, treeUri)!!
val pathList = ArrayList<String>()
listFolderContent(tree).forEach { uri ->
val file = createFileFromContentUri(uri)
pathList.add(file.toString())
}
return pathList
}
private fun listFolderContent(folder: DocumentFile): List<Uri> {
return if (folder.isDirectory) {
val files = folder.listFiles().toMutableList()
files.sortByDescending { it.lastModified() }
files.mapNotNull { file ->
if (file.name != null) file.uri else null
}
} else {
emptyList()
}
}
#RequiresApi(Build.VERSION_CODES.O)
private fun createFileFromContentUri(fileUri: Uri): File {
var fileName = ""
fileUri.let { returnUri ->
context.contentResolver.query(returnUri, null, null, null)
}?.use { cursor ->
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
cursor.moveToFirst()
fileName = cursor.getString(nameIndex)
}
val iStream: InputStream =
context.contentResolver.openInputStream(fileUri)!!
val outputDir: File = context.cacheDir!!
val outputFile = File(outputDir, fileName)
copyStreamToFile(iStream, outputFile)
iStream.close()
return outputFile
}
private fun copyStreamToFile(inputStream: InputStream, outputFile: File) {
inputStream.use { input ->
val outputStream = FileOutputStream(outputFile)
outputStream.use { output ->
val buffer = ByteArray(4 * 1024) // buffer size
while (true) {
val byteCount = input.read(buffer)
if (byteCount < 0) break
output.write(buffer, 0, byteCount)
}
output.flush()
}
}
}
Using DocumentFile to handle SAF uries is slow indeed.
Better use DocumentsContract to do so.
Its about twenty times as fast as DocumentFile and about as fast as classic File class stuff.
Using MediaStore for hidden folders should be possible. You cannot create hidden folders with the mediastore. But if you managed to make them not using mediastore you should be able to list files in them using mediastore. Well if they are scanned. And if they belong to your app.
I have a cartActivity and when I click the proceed button it goes to PaymentActivity. Now, If I click baack button from the paymentActivity I want to go back to the CartActivity.
CartActivity
class CartActivity : AppCompatActivity() {
lateinit var toolbar: androidx.appcompat.widget.Toolbar
lateinit var txtOrderingFrom: TextView
lateinit var btnPlaceOrder: Button
lateinit var recyclerView: RecyclerView
lateinit var layoutManager: RecyclerView.LayoutManager
lateinit var menuAdapter: CartAdapter
lateinit var restaurantId: String
lateinit var restaurantName: String
lateinit var selectedItemsId: ArrayList<String>
lateinit var linearLayout: LinearLayout
lateinit var cartProgressLayout: RelativeLayout
var totalAmount = 0
var cartListItems = arrayListOf<CartItems>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cart)
btnPlaceOrder = findViewById(R.id.btnPlaceOrder)
txtOrderingFrom = findViewById(R.id.txtOrderingFrom)
linearLayout = findViewById(R.id.linearLayout)
toolbar = findViewById(R.id.toolBar)
cartProgressLayout = findViewById(R.id.cartProgressLayout)
restaurantId = intent.getStringExtra("restaurantId").toString()
restaurantName = intent.getStringExtra("restaurantName").toString()
selectedItemsId = intent.getStringArrayListExtra("selectedItemsId") as ArrayList<String>
txtOrderingFrom.text = restaurantName
setToolBar()
fetchData()
btnPlaceOrder.setOnClickListener {
val sharedPreferences = this.getSharedPreferences(
getString(R.string.shared_preferences),
Context.MODE_PRIVATE
)
if (ConnectionManager().checkConnectivity(this)) {
cartProgressLayout.visibility = View.VISIBLE
try {
val foodArray = JSONArray()
for (foodItem in selectedItemsId) {
val singleItemObject = JSONObject()
singleItemObject.put("food_item_id", foodItem)
foodArray.put(singleItemObject)
}
val sendOrder = JSONObject()
sendOrder.put("user_id", sharedPreferences.getString("user_id", "0"))
sendOrder.put("restaurant_id", restaurantId)
sendOrder.put("total_cost", totalAmount)
sendOrder.put("food", foodArray)
val queue = Volley.newRequestQueue(this)
val url = "http://13.235.250.119/v2/place_order/fetch_result"
val jsonObjectRequest = object : JsonObjectRequest(
Method.POST,
url,
sendOrder,
Response.Listener {
val response = it.getJSONObject("data")
val success = response.getBoolean("success")
val LAUNCH_SECOND_ACTIVITY = 1
if (success) {
val intent = Intent(this, PaymentActivity::class.java)
intent.putExtra("total_amount",totalAmount)
startActivityForResult(intent,LAUNCH_SECOND_ACTIVITY)
finishAffinity()
} else {
val responseMessageServer =
response.getString("errorMessage")
Toast.makeText(
this,
responseMessageServer.toString(),
Toast.LENGTH_SHORT
).show()
}
cartProgressLayout.visibility = View.INVISIBLE
},
Response.ErrorListener {
Toast.makeText(
this,
"Some Error occurred!!!",
Toast.LENGTH_SHORT
).show()
}) {
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers["Content-type"] = "application/json"
headers["token"] = "9bf534118365f1"
return headers
}
}
queue.add(jsonObjectRequest)
} catch (e: JSONException) {
Toast.makeText(
this,
"Some unexpected error occurred!!",
Toast.LENGTH_SHORT
).show()
}
} else {
val alterDialog = androidx.appcompat.app.AlertDialog.Builder(this)
alterDialog.setTitle("No Internet")
alterDialog.setMessage("Check Internet Connection!")
alterDialog.setPositiveButton("Open Settings") { _, _ ->
val settingsIntent = Intent(Settings.ACTION_SETTINGS)
startActivity(settingsIntent)
}
alterDialog.setNegativeButton("Exit") { _, _ ->
finishAffinity()
}
alterDialog.setCancelable(false)
alterDialog.create()
alterDialog.show()
}
}
layoutManager = LinearLayoutManager(this)
recyclerView = findViewById(R.id.recyclerViewCart)
}
fun fetchData() {
if (ConnectionManager().checkConnectivity(this)) {
cartProgressLayout.visibility = View.VISIBLE
try {
val queue = Volley.newRequestQueue(this)
val url = "http://13.235.250.119/v2/restaurants/fetch_result/$restaurantId"
val jsonObjectRequest = #SuppressLint("SetTextI18n")
object : JsonObjectRequest(
Method.GET,
url,
null,
Response.Listener {
val response = it.getJSONObject("data")
val success = response.getBoolean("success")
if (success) {
val data = response.getJSONArray("data")
cartListItems.clear()
totalAmount = 0
for (i in 0 until data.length()) {
val cartItem = data.getJSONObject(i)
if (selectedItemsId.contains(cartItem.getString("id"))) {
val menuObject = CartItems(
cartItem.getString("id"),
cartItem.getString("name"),
cartItem.getString("cost_for_one"),
cartItem.getString("restaurant_id")
)
totalAmount += cartItem.getString("cost_for_one").toString()
.toInt()
cartListItems.add(menuObject)
}
menuAdapter = CartAdapter(this, cartListItems)
recyclerView.adapter = menuAdapter
recyclerView.layoutManager = layoutManager
}
btnPlaceOrder.text = "Place Order(Total: Rs. $totalAmount)"
}
cartProgressLayout.visibility = View.INVISIBLE
},
Response.ErrorListener {
Toast.makeText(
this,
"Some Error occurred!!!",
Toast.LENGTH_SHORT
).show()
cartProgressLayout.visibility = View.INVISIBLE
}) {
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers["Content-type"] = "application/json"
headers["token"] = "26c5144c5b9c13"
return headers
}
}
queue.add(jsonObjectRequest)
} catch (e: JSONException) {
Toast.makeText(
this,
"Some Unexpected error occurred!!!",
Toast.LENGTH_SHORT
).show()
}
} else {
val alterDialog = androidx.appcompat.app.AlertDialog.Builder(this)
alterDialog.setTitle("No Internet")
alterDialog.setMessage("Check Internet Connection!")
alterDialog.setPositiveButton("Open Settings") { _, _ ->
val settingsIntent = Intent(Settings.ACTION_SETTINGS)
startActivity(settingsIntent)
}
alterDialog.setNegativeButton("Exit") { _, _ ->
finishAffinity()
}
alterDialog.setCancelable(false)
alterDialog.create()
alterDialog.show()
}
}
fun setToolBar() {
setSupportActionBar(toolbar)
supportActionBar?.title = "My Cart"
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_back_arrow)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
super.onBackPressed()
}
}
return super.onOptionsItemSelected(item)
}
}
PaymentActivity
class PaymentActivity : AppCompatActivity() {
var amountEt: TextView? = null
var nameEt: TextView? = null
var upiIdEt: TextView? = null
var send: Button? = null
val UPI_PAYMENT = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_payment)
initializeViews()
send?.setOnClickListener {
payUsingUpi("1", "sruthikrithika0#okhdfcbank", "Sruthi")
}
}
fun initializeViews() {
send = findViewById(R.id.send)
amountEt = findViewById(R.id.amount_et)
nameEt = findViewById(R.id.upi_name)
upiIdEt = findViewById(R.id.upi_id)
}
fun payUsingUpi(amount: String?, upiId: String?, name: String?) {
val uri: Uri = Uri.parse("upi://pay").buildUpon()
.appendQueryParameter("pa", upiId)
.appendQueryParameter("pn", name)
.appendQueryParameter("am", amount)
.appendQueryParameter("cu", "INR")
.build()
val upiPayIntent = Intent(Intent.ACTION_VIEW)
upiPayIntent.data = uri
// will always show a dialog to user to choose an app
val chooser = Intent.createChooser(upiPayIntent, "Pay with")
// check if intent resolves
if (null != chooser.resolveActivity(packageManager)) {
startActivityForResult(chooser, UPI_PAYMENT)
} else {
Toast.makeText(
this#PaymentActivity,
"No UPI app found, please install one to continue",
Toast.LENGTH_SHORT
).show()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
UPI_PAYMENT -> if (RESULT_OK == resultCode || resultCode == 11) {
if (data != null) {
val trxt = data.getStringExtra("response")
Log.d("UPI", "onActivityResult: $trxt")
val dataList: ArrayList<String?> = ArrayList()
dataList.add(trxt)
upiPaymentDataOperation(dataList)
} else {
Log.d("UPI", "onActivityResult: " + "Return data is null")
val dataList: ArrayList<String?> = ArrayList()
dataList.add("nothing")
upiPaymentDataOperation(dataList)
}
} else {
Log.d(
"UPI",
"onActivityResult: " + "Return data is null"
) //when user simply back without payment
val dataList: ArrayList<String?> = ArrayList()
dataList.add("nothing")
upiPaymentDataOperation(dataList)
}
}
}
private fun upiPaymentDataOperation(data: ArrayList<String?>) {
if (isConnectionAvailable(this#PaymentActivity)) {
var str = data[0]
Log.d("UPIPAY", "upiPaymentDataOperation: $str")
var paymentCancel = ""
if (str == null) str = "discard"
var status = ""
var approvalRefNo = ""
val response = str.split("&").toTypedArray()
for (i in response.indices) {
val equalStr = response[i].split("=").toTypedArray()
if (equalStr.size >= 2) {
if (equalStr[0].toLowerCase() == "Status".toLowerCase()) {
status = equalStr[1].toLowerCase()
} else if (equalStr[0].toLowerCase() == "ApprovalRefNo".toLowerCase() || equalStr[0].toLowerCase() == "txnRef".toLowerCase()) {
approvalRefNo = equalStr[1]
}
} else {
paymentCancel = "Payment cancelled by user."
}
}
if (status == "success") {
//Code to handle successful transaction here.
Toast.makeText(this#PaymentActivity, "Transaction successful.", Toast.LENGTH_SHORT)
.show()
Log.d("UPI", "responseStr: $approvalRefNo")
} else if ("Payment cancelled by user." == paymentCancel) {
Toast.makeText(this#PaymentActivity, "Payment cancelled by user.", Toast.LENGTH_SHORT)
.show()
} else {
Toast.makeText(
this#PaymentActivity,
"Transaction failed.Please try again",
Toast.LENGTH_SHORT
).show()
}
} else {
Toast.makeText(
this#PaymentActivity,
"Internet connection is not available. Please check and try again",
Toast.LENGTH_SHORT
).show()
}
}
companion object {
fun isConnectionAvailable(context: Context): Boolean {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (connectivityManager != null) {
val netInfo = connectivityManager.activeNetworkInfo
if (netInfo != null && netInfo.isConnected
&& netInfo.isConnectedOrConnecting
&& netInfo.isAvailable
) {
return true
}
}
return false
}
}
}
Now, when I click back button from PaymentActivity the app crashes because the same state in the cartActivity is not present.How to resolve this ??
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.foodly">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:networkSecurityConfig="#xml/network_security_config"
android:resizeableActivity="false"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.Foodly">
<activity android:name=".activity.OrderPlacedActivity"></activity>
<activity android:name=".activity.CartActivity" />
<activity android:name=".activity.OrderHistoryActivity" />
<activity
android:name=".activity.RestaurantMenuActivity" />
<activity
android:name=".activity.RegisterActivity" />
<activity
android:name=".activity.LoginActivity" />
<activity
android:name=".activity.PaymentActivity"/>
<activity
android:name=".activity.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.MainActivity" />
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
</manifest>
Why do you call startActivity() in your OnBackPressed() callback ? You don't have to, it's automatic. If you need to retrieve parameters in the CartActivity, you have to launch the PaymentActivity with startActivityForResult()
Problem is with the
finishAffinity()
finishAffinity() is used to remove a number of Activitys belonging to a specific application from the current task (which may contain Activitys belonging to multiple applications).
You shouldn't be doing any finish on starting payment activity. you should be using finishAffinity() after payment success so the last activities should not come on back press.
For more info - https://developer.android.com/reference/android/app/Activity#finishAffinity()
I am getting a class not found exception when trying to click an image from the camera or while fetching it from the directory. The code is in kotlin and has been given below.
This is the class which implements the functionality to capture an image or picks them from the gallery.
The methods _openCamera() and openFileSelector() are implemented.
The main motive was to capture the images and upload them in the server but the implemented methods doesn't give the proper results.
class MainActivity : AppCompatActivity() {
private var drawerResult: Drawer? = null
private var jobschedular: JobScheduler? = null
private var jobschedularCode: Int = 1
private var phoneNumber: String? = null
private var toolbar: Toolbar? = null
private var familyId: String? = null
val TAG: String? = "Activity_Name"
val REQUEST_IMAGE_CAPTURE = 1
val REQUEST_CODE_FOR_GALLERY_CAPTURE = 2
var photoFile: File? = null
var progressDialog: Dialog? = null
private var doubleBackToExitPressedOnce = false
#SuppressLint("PrivateResource")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
Log.d(TAG, "Inside MainActivity")
//onclick listener for open camera
onclickListenerForOpenCamera()
//starting the services here . .
val service_checkAddedtoFamily = Intent(this, checkAddedToFamily::class.java)
startService(service_checkAddedtoFamily)
val service_checkDocsToBeVerified = Intent(this, checkDocsToBeVerified::class.java)
startService(service_checkDocsToBeVerified)
/*findViewById<Button>(R.id.scan).setOnClickListener {
val i = Intent(this, Testers::class.java)
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
startActivity(i)
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
}*/
//onclick listener for select image button
attach_onclick_listener_to_add_photos_from_gallery()
//onclick listener for select pdf files
onclickListenerForSelectPdfFile()
//get toolbar for drawer
toolbar = findViewById(R.id.toolbar_tabs)
//get phone number
val loginInfo = applicationContext.getSharedPreferences("loginInfo", Context.MODE_PRIVATE)
phoneNumber = loginInfo.getString("phoneNumber", "")
//onclick listener for upload button
//onclickListenerForUploadButton()
//onclick listener for retrieve button
onclickListenerForRetrieveButton()
//on click permanent diseases button
//onclickPermanentDiseasesButtton()
//navigation drawer
left_drawer(this, this#MainActivity, toolbar!!).createNavigationDrawer()
//verify auto upload
verifyAutoLoginInformation()
//create Sqlite database
DB_HELPER(this#MainActivity).writableDatabase
//get job schedular service
jobschedular = applicationContext.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
schedulTheJobForHealthGoals()
schedulTheJobForHealthInsurance()
setPreferencesForNutrition()
schedulTheJobForNutrition()
schedulTheJobForSyncNutritionOnline()
}
/*override fun onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed()
return
}
this.doubleBackToExitPressedOnce = true
Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show()
Handler().postDelayed(Runnable { doubleBackToExitPressedOnce = false }, 2000)
}*/
//job schedular
fun schedulTheJobForHealthGoals() {
val builder = JobInfo.Builder(jobschedularCode, ComponentName(this#MainActivity, health_goals_services::class.java))
.setPersisted(true)
.setPeriodic(5000)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setRequiresCharging(false)
.setRequiresDeviceIdle(false)
val bundle = PersistableBundle()
bundle.putString("key", "value")
builder.setExtras(bundle)
val s_response = jobschedular!!.schedule(builder.build())
if (s_response <= 0) {
//something goes wrong
}
}
fun schedulTheJobForHealthInsurance() {
val builder = JobInfo.Builder(jobschedularCode, ComponentName(this#MainActivity, health_insurance_service::class.java))
.setPersisted(true)
.setPeriodic(5000)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setRequiresCharging(false)
.setRequiresDeviceIdle(false)
val bundle = PersistableBundle()
bundle.putString("key", "value")
builder.setExtras(bundle)
val s_response = jobschedular!!.schedule(builder.build())
if (s_response <= 0) {
//something goes wrong
}
}
fun schedulTheJobForNutrition() {
val builder = JobInfo.Builder(jobschedularCode, ComponentName(this#MainActivity, nutrition_service::class.java))
.setPersisted(true)
.setPeriodic(5000) //change to 1 hour
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setRequiresCharging(false)
.setRequiresDeviceIdle(false)
val bundle = PersistableBundle()
bundle.putString("key", "value")
builder.setExtras(bundle)
val s_response = jobschedular!!.schedule(builder.build())
if (s_response <= 0) {
//something goes wrong
}
}
fun setPreferencesForNutrition() {
val nutritionInfo = getSharedPreferences("nutrition", Context.MODE_PRIVATE)
val editor = nutritionInfo.edit()
editor.putString("breakFastTime_Hour", "7")
editor.putString("lunchTime_Hour", "14") //TODO: change to 13
editor.putString("DinnerTime_Hour", "20")
editor.apply()
}
fun schedulTheJobForSyncNutritionOnline() {
val builder = JobInfo.Builder(jobschedularCode, ComponentName(this#MainActivity, sync_nutrition_online::class.java))
.setPersisted(true)
.setPeriodic(5000)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setRequiresCharging(false)
.setRequiresDeviceIdle(false)
val bundle = PersistableBundle()
bundle.putString("key", "value")
builder.setExtras(bundle)
val s_response = jobschedular!!.schedule(builder.build())
if (s_response <= 0) {
//something goes wrong
}
}
//buttons on home screen
/*fun onclickListenerForUploadButton(){
findViewById<ImageView>(R.id.uploadButton).setOnClickListener{
openModeOfUploadActivity()
}
}*/
fun onclickListenerForRetrieveButton() {
findViewById<Button>(R.id.retrieveButton).setOnClickListener {
openHistoryActivity()
}
}
/*fun onclickPermanentDiseasesButtton(){
findViewById<Button>(R.id.permanentDiseasesButton).setOnClickListener{
openPermanentDiseases()
}
}*/
/*fun openModeOfUploadActivity(){
val intent = Intent(this,MainActivity::class.java)
startActivity(intent)
}*/
fun openHistoryActivity() {
val intent = Intent(this, history_pickFamilyMember::class.java)
startActivity(intent)
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
}
/*fun openPermanentDiseases(){
val intent = Intent(this,permanentDiseaese::class.java)
startActivity(intent)
}
*/
//verify auto login information
fun verifyAutoLoginInformation() {
val loginInfo = applicationContext.getSharedPreferences("loginInfo", Context.MODE_PRIVATE)
if (loginInfo.contains("familyOrIndividual") == true) {
//for family
if (loginInfo.getString("familyOrIndividual", "").toString() == "f") {
if (loginInfo.contains("phoneNumber") == true && loginInfo.contains("password") == true) {
val phoneNumber = loginInfo.getString("phoneNumber", "")
val password = loginInfo.getString("password", "")
individual_family_login(this#MainActivity).makeFamilyLoginApiRequest(phoneNumber, password)
} else {
left_drawer(this, this#MainActivity, toolbar!!).makeUserLogOut()
}
}
//for individual
if (loginInfo.getString("familyOrIndividual", "").toString() == "i") {
if (loginInfo.contains("phoneNumber") == true && loginInfo.contains("password") == true) {
val phoneNumber = loginInfo.getString("phoneNumber", "")
val password = loginInfo.getString("password", "")
individual_family_login(this#MainActivity).makeLoginApiRequest(phoneNumber, password)
} else {
left_drawer(this, this#MainActivity, toolbar!!).makeUserLogOut()
}
}
//for security
if (loginInfo.getString("familyOrIndividual", "").toString() != "i" && loginInfo.getString("familyOrIndividual", "").toString() != "f") {
left_drawer(this, this#MainActivity, toolbar!!).makeUserLogOut()
}
} else {
left_drawer(this, this#MainActivity, toolbar!!).makeUserLogOut()
}
}
//camera scan
fun onclickListenerForOpenCamera() {
findViewById<ImageView>(R.id.openCamera).setOnClickListener {
get_permissions_camera()
}
}
fun _openCamera() {
Log.d("Errors__", "inside _openCamera()")
try {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
try {
photoFile = createImageFile()
} catch (ex: Exception) {
Log.d("Errors__", "inside: " + ex.toString())
}
if (photoFile != null) {
val builder: StrictMode.VmPolicy.Builder = StrictMode.VmPolicy.Builder()
StrictMode.setVmPolicy(builder.build())
val photoURI: Uri = Uri.fromFile(photoFile!!)
Log.d("Path__", "photoURI: $photoURI")
Log.d("Path__", "photoURI.path: " + photoURI.path)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
}
}
} catch (e: Exception) {
Log.d("Errors__", "_openCamera" + e.toString())
}
}
fun createImageFile(): File {
Log.d("Errors__", "inside createImageFile()")
val mCurrentPhotoPath: String
val imageFileName = "camera"
val storageDir: File = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
)
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath()
Log.d("Path__", "Image: $image")
return image
}
//file selector
fun onclickListenerForSelectPdfFile() {
findViewById<ImageView>(R.id.selectPdfFile).setOnClickListener {
get_permissions_fileExplorer()
}
}
#SuppressLint("SdCardPath")
fun openFileSelector() {
val properties = DialogProperties()
properties.selection_mode = DialogConfigs.MULTI_MODE;
properties.selection_type = DialogConfigs.FILE_SELECT;
properties.root = File(DialogConfigs.DEFAULT_DIR);
properties.error_dir = File(DialogConfigs.DEFAULT_DIR);
properties.offset = File(DialogConfigs.DEFAULT_DIR);
properties.extensions = null;
val dialog: FilePickerDialog = FilePickerDialog(this#MainActivity, properties)
dialog.setTitle("Select a File")
dialog.setDialogSelectionListener(object : DialogSelectionListener {
override fun onSelectedFilePaths(files: Array<out String>?) {
convertPdfToImages(files!!)
}
})
dialog.show()
}
fun convertPdfToImages(files: Array<out String>) {
showProcessProgress()
doAsync {
var uriList: MutableList<Uri>? = mutableListOf()
val no_of_files = files.size
var counter = 0
while (counter < no_of_files) {
var pdfFile = File(files[counter])
val decodeService = DecodeServiceBase(PdfContext())
decodeService.setContentResolver(applicationContext.getContentResolver())
decodeService.open(Uri.fromFile(pdfFile))
val pageCount: Int = decodeService.getPageCount()
var i = 0
while (i < pageCount) {
val page: PdfPage = decodeService.getPage(i) as PdfPage
val rectF = RectF(0.toFloat(), 0.toFloat(), 1.toFloat(), 1.toFloat())
// do a fit center to 1920x1080
val scaleBy = 1
val with: Int = (page.getWidth() * scaleBy)
val height: Int = (page.getHeight() * scaleBy)
val bitmap: Bitmap = page.renderBitmap(with, height, rectF)
try {
val outputFile = File(applicationContext.externalCacheDir,
System.currentTimeMillis().toString() + ".jpg")
val outputStream = FileOutputStream(outputFile)
// a bit long running
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
uriList!!.add(Uri.fromFile(outputFile))
outputStream.close()
} catch (e: IOException) {
}
i++
}
counter++
}
uiThread {
progressDialog!!.hide()
openPreview(uriList!!)
Log.d("mess", "size: " + uriList.size + " " + uriList.toString())
}
}
}
//select image
fun attach_onclick_listener_to_add_photos_from_gallery() {
findViewById<ImageView>(R.id.selectImage).setOnClickListener {
get_permissions_gallery()
}
}
fun open_selector() {
Matisse.from(this)
.choose(MimeType.allOf())
.countable(true)
.maxSelectable(200)
.restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
.thumbnailScale(0.85f)
.imageEngine(PicassoEngine())
.forResult(REQUEST_CODE_FOR_GALLERY_CAPTURE)
}
//activity results
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
var uriOfImage = Uri.fromFile(photoFile)
Log.d("fileCapturing__", "URI Image: $uriOfImage")
//start croper
CropImage.activity(uriOfImage)
.start(this)
}
if (requestCode == REQUEST_CODE_FOR_GALLERY_CAPTURE && resultCode == Activity.RESULT_OK) {
var selected_images = Matisse.obtainResult(data)
openPreview(selected_images!!)
}
//for croper
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
val result: CropImage.ActivityResult = CropImage.getActivityResult(data)
if (resultCode == RESULT_OK) {
doAsync {
val builder: StrictMode.VmPolicy.Builder = StrictMode.VmPolicy.Builder()
StrictMode.setVmPolicy(builder.build())
var resultUri: Uri = result.getUri()
//save cropped image for persisitance
val croppedImage = createImageFile() //empty
val outputStream = FileOutputStream(croppedImage)
// a bit long running
(Picasso.with(this#MainActivity)
.load(resultUri)
.get()
).compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
Log.d("fileCapturing__", "outputStream: $outputStream")
resultUri = Uri.fromFile(croppedImage)
outputStream.close()
uiThread {
//add to mu list
var mu_list = ArrayList<Uri>(1)
mu_list.add(resultUri)
Log.d("fileCapturing__", "camera uri" + resultUri.toString())
openPreview(mu_list)
}
}
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
val error: Exception = result.getError()
Log.d("fileCapturing__", "Error: $error")
}
}
}
//preview
fun openPreview(list: MutableList<Uri>) {
val _object = list
val i = Intent(this, typeOfDocument::class.java)
val args = Bundle()
args.putSerializable("ARRAYLIST", _object as java.io.Serializable)
i.putExtra("BUNDLE", args)
startActivity(i)
finish()
}
//get permissions
//Camera
fun get_permissions_camera() {
if (ContextCompat.checkSelfPermission(this#MainActivity, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
MaterialDialog.Builder(this#MainActivity)
.title("Camera permission")
.content("Camera permissions are required for opening Camera")
.negativeText("Cancel")
.onNegative(object : MaterialDialog.SingleButtonCallback {
override fun onClick(dialog: MaterialDialog, which: DialogAction) {
}
})
.positiveText("Give Permissions")
.onPositive(object : MaterialDialog.SingleButtonCallback {
override fun onClick(dialog: MaterialDialog, which: DialogAction) {
getPermissionsUsingDexter_camera(
android.Manifest.permission.CAMERA
)
}
})
.show()
} else {
_openCamera()
}
}
fun getPermissionsUsingDexter_camera(permissionString: String) {
Dexter.withActivity(this)
.withPermissions(
permissionString
).withListener(object : MultiplePermissionsListener {
override fun onPermissionRationaleShouldBeShown(permissions: MutableList<PermissionRequest>?, token: PermissionToken?) {
}
override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
if (report!!.areAllPermissionsGranted() == true) {
_openCamera()
Log.d("mess", "permission given")
} else {
Log.d("mess", "permission not granted")
}
}
})
.check()
}
//gallery
fun get_permissions_gallery() {
if (ContextCompat.checkSelfPermission(this#MainActivity, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
MaterialDialog.Builder(this#MainActivity)
.title("Storage permission")
.content("Storage permissions are required for opening the Gallery")
.negativeText("Cancel")
.onNegative(object : MaterialDialog.SingleButtonCallback {
override fun onClick(dialog: MaterialDialog, which: DialogAction) {
}
})
.positiveText("Give Permissions")
.onPositive(object : MaterialDialog.SingleButtonCallback {
override fun onClick(dialog: MaterialDialog, which: DialogAction) {
getPermissionsUsingDexter_gallery(
android.Manifest.permission.READ_EXTERNAL_STORAGE
)
}
})
.show()
} else {
open_selector()
}
}
fun getPermissionsUsingDexter_gallery(permissionString: String) {
Dexter.withActivity(this)
.withPermissions(
permissionString
).withListener(object : MultiplePermissionsListener {
override fun onPermissionRationaleShouldBeShown(permissions: MutableList<PermissionRequest>?, token: PermissionToken?) {
}
override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
if (report!!.areAllPermissionsGranted() == true) {
open_selector()
Log.d("mess", "permission given")
} else {
Log.d("mess", "permission not granted")
}
}
})
.check()
}
//file exploer
fun get_permissions_fileExplorer() {
if (ContextCompat.checkSelfPermission(this#MainActivity, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
MaterialDialog.Builder(this#MainActivity)
.title("Storage permission")
.content("Storage access permissions are required for opening File Explorer")
.negativeText("Cancel")
.onNegative(object : MaterialDialog.SingleButtonCallback {
override fun onClick(dialog: MaterialDialog, which: DialogAction) {
}
})
.positiveText("Give Permissions")
.onPositive(object : MaterialDialog.SingleButtonCallback {
override fun onClick(dialog: MaterialDialog, which: DialogAction) {
getPermissionsUsingDexter_fileExplores(
android.Manifest.permission.READ_EXTERNAL_STORAGE
)
}
})
.show()
} else {
openFileSelector()
}
}
fun getPermissionsUsingDexter_fileExplores(permissionString: String) {
Dexter.withActivity(this)
.withPermissions(
permissionString
).withListener(object : MultiplePermissionsListener {
override fun onPermissionRationaleShouldBeShown(permissions: MutableList<PermissionRequest>?, token: PermissionToken?) {
}
override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
if (report!!.areAllPermissionsGranted() == true) {
openFileSelector()
Log.d("mess", "permission given")
} else {
Log.d("mess", "permission not granted")
}
}
})
.check()
}
//progress bar
fun showProcessProgress() {
progressDialog = MaterialDialog.Builder(this)
.title("Please Wait")
.content("Converting Pdf to Images")
.progress(true, 0)
.show()
}
}
The error shows when I try to click an image or fetch from the library.
The provider path used is given below:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
I have tried changing the path to "/" but it didn't work. The error wasn't showing earlier but the error exists now.
Here is the snapshot of the logs.
All suggestions are accepted. Thanks in advance.