V
V
VitGun2020-11-18 19:45:59
Android
VitGun, 2020-11-18 19:45:59

Android 10(SDK ​​29). How to create a directory in DCIM?

Good day. I must say right away that I am not an android developer. Essence. Two years ago, my wife moved to a new job. There it was necessary to photograph various objects, name files with pictures in a special way and further process them. She took the photo with her phone. Renaming took a lot of time and was a chore. I bungled a simple application by touch - it asked for a file number (with a prefix prefixed), opened the camera, took a picture and saved with a specific name (prefix + numbers entered by the user). All this economy worked fine on Android 7.0. But then an opportunity happened - the device was changed to Android 10 and the program stopped working. Through witchcraft, sacrifices and fiddling with the debugger, it turned out that the problem is in this piece

private File getOutputMediaFile(String filename)
    {
        File mediaStorageDir = new File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+File.separator+"Camera"+File.separator+filename+File.separator);
        if (!mediaStorageDir.exists()){
            if (!mediaStorageDir.mkdirs()){
                Log.d("AutoCAMERA", "failed to create directory");
                return null;
            }
        }
        return new File(mediaStorageDir.getPath()+File.separator+filename+".jpg");
    }

Here the file name and the directory where to be saved should have been formed. And it worked on the 7th version. On the 10th, it turned out that getExternalStoragePublicDirectory is now deprecated. OK. Digging through Google, I found the magic words,

android:requestLegacyExternalStorage="true"

but this magic did not work. mediaStorageDir.mkdirs() - persistently returns false, i.e. the directory is not created and all further logic flies to hell. Hence all the problems. Rummaged in Google. On SO, in topics of various freshness, it is advised to use the spell android:requestLegacyExternalStorage="true", but it does not work. Again, I'm not an android developer. How to create a directory and write a file there on SDK 29? Flags

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

stand. The third day I bust. Help who than can? Advice, link, article? Maybe a solution?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question