Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

createTempFile() on Android 10 permission denied

When attempting to create a temp file on Android 10 I am getting a permission denied error. Could someone please write a tutorial on how to create one in the external storage? Most are outdated and not for current versions of Android

Here is the logcat

 2020-02-22 13:46:43.766 9544-9544/com.myapp.test E/MainActivity: Image file creation failed
java.io.IOException: Permission denied
    at java.io.UnixFileSystem.createFileExclusively0(Native Method)
    at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
    at java.io.File.createTempFile(File.java:2018)
    at com.myapp.test.MainActivity.create_image(MainActivity.java:915)
    at com.myapp.test.MainActivity.access$200(MainActivity.java:85)
    at com.myapp.test.MainActivity$6.onShowFileChooser(MainActivity.java:513)
    at vo.runFileChooser(PG:10)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:336)
    at android.os.Looper.loop(Looper.java:197)
    at android.app.ActivityThread.main(ActivityThread.java:7762)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)

My AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />

And code in the MainActivity.Java

private File create_image() throws IOException {
    @SuppressLint("SimpleDateFormat")
    String file_name    = new SimpleDateFormat("yyyy_mm_ss").format(new Date());
    String new_name     = "file_"+file_name+"_";
    File sd_directory   = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(new_name, ".jpg", sd_directory); // fails due to permission denied
}
like image 684
Micha Avatar asked Feb 22 '20 20:02

Micha


2 Answers

I was using official documentation to implement taking a photo from camera:

https://developer.android.com/training/camera/photobasics

yet it doesn't work and throws an exception on createTempFile method. Strangely google hasn't updated the code.

Here's a solution I've found:

Add:

android:requestLegacyExternalStorage="true"

to AndroidManifest.xml. No need to change anything in code. Works for now, but may not in upcoming Android versions.

like image 172
Makalele Avatar answered Nov 13 '22 12:11

Makalele


You need to change

File sd_directory   = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

to

File sd_directory   =  getExternalFilesDir(Environment.DIRECTORY_PICTURES);
like image 34
Kaleab O. Avatar answered Nov 13 '22 12:11

Kaleab O.