From b8b8157d046825873f98df86ea9eecd5471bcb68 Mon Sep 17 00:00:00 2001 From: gowtham-13309 Date: Fri, 1 Mar 2024 00:49:15 +0530 Subject: [PATCH] fix: android 14 selected file permission changes --- .../gowtham/videotrimmer/MainActivity.java | 16 +++++++++- library/src/main/AndroidManifest.xml | 1 + .../gowtham/library/ui/ActVideoTrimmer.java | 31 ++++++++++++++----- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/gowtham/videotrimmer/MainActivity.java b/app/src/main/java/com/gowtham/videotrimmer/MainActivity.java index e139885..066122a 100644 --- a/app/src/main/java/com/gowtham/videotrimmer/MainActivity.java +++ b/app/src/main/java/com/gowtham/videotrimmer/MainActivity.java @@ -272,7 +272,21 @@ private long getEdtValueLong(EditText editText) { } private boolean checkCamStoragePer() { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + boolean hasPermission= ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED) + == PackageManager.PERMISSION_GRANTED + || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_VIDEO) + == PackageManager.PERMISSION_GRANTED; + boolean hasCamera= ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) + == PackageManager.PERMISSION_GRANTED; + if(hasPermission && hasCamera){ + return true; + }else { + return checkPermission( + Manifest.permission.READ_MEDIA_VIDEO, Manifest.permission.CAMERA); + } + + } else if(Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU) { return checkPermission( Manifest.permission.READ_MEDIA_VIDEO, Manifest.permission.CAMERA); diff --git a/library/src/main/AndroidManifest.xml b/library/src/main/AndroidManifest.xml index ffdc376..1e8f7b5 100644 --- a/library/src/main/AndroidManifest.xml +++ b/library/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + diff --git a/library/src/main/java/com/gowtham/library/ui/ActVideoTrimmer.java b/library/src/main/java/com/gowtham/library/ui/ActVideoTrimmer.java index 445107c..c4a0118 100644 --- a/library/src/main/java/com/gowtham/library/ui/ActVideoTrimmer.java +++ b/library/src/main/java/com/gowtham/library/ui/ActVideoTrimmer.java @@ -216,7 +216,7 @@ private void setDataInView() { try { Runnable fileUriRunnable = () -> { Uri uri = Uri.parse(bundle.getString(TrimVideo.TRIM_VIDEO_URI)); - String path= FileUtilKt.getValidatedFileUri(ActVideoTrimmer.this,uri); + String path = FileUtilKt.getValidatedFileUri(ActVideoTrimmer.this, uri); filePath = Uri.parse(path); runOnUiThread(() -> { LogMessage.v("VideoUri:: " + uri); @@ -337,7 +337,7 @@ private void loadThumbnails() { try { long diff = totalDuration / 8; int sec = 1; - File videoFile= new File(filePath.toString()); + File videoFile = new File(filePath.toString()); for (ImageView img : imageViews) { long interval = (diff * sec) * 1000000; RequestOptions options = new RequestOptions().frame(interval); @@ -475,8 +475,8 @@ protected void onDestroy() { videoPlayer.release(); if (progressView != null && progressView.isShowing()) progressView.dismiss(); - File f=new File(getCacheDir(), "temp_video_file"); - if(f.exists()){ + File f = new File(getCacheDir(), "temp_video_file"); + if (f.exists()) { f.delete(); } stopRepeatingTask(); @@ -697,12 +697,27 @@ private void showProcessingDialog() { } private boolean checkStoragePermission() { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) - { + Uri uri = Uri.parse(bundle.getString(TrimVideo.TRIM_VIDEO_URI)); + String fileUri= FileUtilKt.getActualFileUri(this, uri); + if(fileUri!=null && new File(fileUri).canRead()){ + return true; + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + boolean hasPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED) + == PackageManager.PERMISSION_GRANTED || + ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_VIDEO) + == PackageManager.PERMISSION_GRANTED; + if (hasPermission) { + return true; + } else { + return checkPermission( + Manifest.permission.READ_MEDIA_VIDEO); + } + } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU) { return checkPermission( Manifest.permission.READ_MEDIA_VIDEO); - } - else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { return checkPermission( Manifest.permission.READ_EXTERNAL_STORAGE); } else