0

I have a video recording app that records and saving it in the External Storage,specifically the app-specific-storage one /storage/emulated/0/Android/data/com.example.xxxx/files

I used the context.getExternalFilesDir(Environment.DIRECTORY_MOVIES) to save it to the file path /storage/emulated/0/Android/data/com.example.xxxx/files/Movies.

Reading the video files on the context.getExternalFilesDir(Environment.DIRECTORY_MOVIES) directory works. I am able to print the File name. However I cant access and open the actual file. I'm using this function to retrieve the video file.

public static File getAppSpecificVideoFile(Context context, String fileName) {
        // external storage.
        File file = new File(context.getExternalFilesDir(
                Environment.DIRECTORY_MOVIES), fileName);
       
        return file;
    }

So the whole video path with will be /storage/emulated/0/Android/data/com.example.xxxx/files/Movies/filename.3gp

I have already allowed required permissions.

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

And enabled the android:requestLegacyExternalStorage="true"

Also used this method to request for permissions

  private void requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            try {
                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setData(Uri.parse(String.format("package:%s",getApplicationContext().getPackageName())));
                startActivityForResult(intent, 2296);
            } catch (Exception e) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                startActivityForResult(intent, 2296);
            }
        } else {
            //below android 11
            ActivityCompat.requestPermissions(this, new String[]{WRITE_EXTERNAL_STORAGE,  Manifest.permission.MANAGE_EXTERNAL_STORAGE}, 2296);
        }
    }

private boolean checkPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        return Environment.isExternalStorageManager();
    } else {
        int result = ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE);
        int result1 = ContextCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
    }
}

EDIT My problem was accessing files using Intent Intent.ACTION_VIEW. According to the Docs.

On Android 11, apps can no longer access files in any other app's dedicated, app-specific directory within external storage.

My solution was to create my own video player like ExoPlayer.

Matthew Nantes
  • 98
  • 2
  • 10
  • `I'm using this function to retrieve the video file.` Ok. You wanna see it that file exists. `if (file == null || !file.mkdirs()) { Log.e(TAG, "Directory not created");` ??? Why are you suddenly creating a directory for a file? Makes no sense. – blackapps Apr 26 '21 at 07:24
  • Ops my bad. The error log is correct. But my concern was I can't open and access file. I was trying to open the viideo using an Intent/Using a 3rd party like vlc to play it. – Matthew Nantes Apr 26 '21 at 10:05
  • Then rewrite your post and remove all this mkdir with successive errors stuff please. – blackapps Apr 26 '21 at 10:08
  • However, It caused me to implement my own video player on my app(ExoPlayer). I assume using an outside app even the usage of intent prevents it from playing the actual video." On Android 11, apps can no longer access files in any other app's dedicated, app-specific directory within external storage." – Matthew Nantes Apr 26 '21 at 10:08
  • Where are you talking about? Dont try to create directories if you try to open a file. You create a directory with the name of the file. – blackapps Apr 26 '21 at 10:09
  • `public static File getAppSpecificVideoStorageDir(Context context, String fileName)` No need for a fileName parameter. Change to `public static File getAppSpecificVideoStorageDir(Context context)` to prevent your sorrows. – blackapps Apr 26 '21 at 10:12
  • That code was derived from the Docs https://developer.android.com/training/data-storage/app-specific#media – Matthew Nantes Apr 26 '21 at 10:20
  • I will not follow that link but i'm surprised you still dont realize you create a directory with the same name as your file would have.. That makes no sense. – blackapps Apr 26 '21 at 10:23
  • "I'm using this function to retrieve the video file." I literally said thats what i use to get the video. Probably the function name was wrong. it should be "getAppSpecificFile" but you pointing out the flaw doesn't help answering the question. – Matthew Nantes Apr 26 '21 at 10:30

0 Answers0