3

I am trying to create a file on the file system, but I still get this exception:

private fun getTempFolder(): File {
        val directoryFolder =
            File(Environment.getExternalStorageDirectory(), "sample-take-image")
        directoryFolder.mkdirs()
        return directoryFolder
    }
    private fun getTempFile(): File {
        val timeStamp = SimpleDateFormat("yyyyMMdd_HHMMss", Locale.getDefault()).format(Date())
        return File(
            getTempFolder().absolutePath,
            "image".plus(Calendar.getInstance().timeInMillis).plus(timeStamp).plus(".jpg")
        )
    }
    private fun saveImageToFile(bitmap: Bitmap? = null): String? {
        return try {
            val file = getTempFile()
            Timber.e("Path: + : ${file.absolutePath}")
            file.createNewFile()
            val fOut = FileOutputStream(file)
            bitmap?.apply {
                this.compress(Bitmap.CompressFormat.JPEG, 100, fOut)
            }
            fOut.flush()
            fOut.close()
            file?.absolutePath.getDefault()
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

Even though I got the link after taking the photo camare:

: /storage/emulated/0/sample-take-image/image162064003789420210510_160517.jpg

So can someone tell me why I can't create the file in this case. I am currently on Android 11 with

**compileSdkVersion 30

buildToolsVersion "30.0.2"

minSdkVersion 26**
1
  • directoryFolder.mkdirs() Bad coding. Better: if ( ! directoryFoldes.exists() if ( !directoryFolder.mkdirs()) return; Commented May 10, 2021 at 10:09

4 Answers 4

9

On An Android 11 device you cannot create directories or files in root of external storage.

But you can in all public directories that are already there.

File(Environment.getExternalStorageDirectory(), "sample-take-image")

Replace for instance by:

File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "sample-take-image")
Sign up to request clarification or add additional context in comments.

Comments

2

Use getExternalFilesDir(), getExternalCacheDir(), or getExternalMediaDirs() (methods on Context), instead of Environment.getExternalStorageDirectory() as this is deprecated.

Comments

2
String dirPath;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath() + "/" + getString(R.string.app_name) + "/";
            } else {
                dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getString(R.string.app_name) + "/";
            }

Comments

0

In my Case, permission was missing

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

Also request to Allow the Storage Permission in the App Setting

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.