2

I am new to Android and mostly using snippets of code from other posts to build my project. I am having a hard time creating a new directory and file on my device. I am using the following code, but I am unable to verify the success of the creation of this path. I want to be able to mount my phone to my laptop and find a file named "MyRecording.pcm" in a folder "/My/Files". I am using the boolean value of mkdirs() to verify whether or not the path was created on my device. If that path was not created then my TextView will tell me "Directories do not exist"; otherwise, my code will create the file MyRecording.pcm. I keep getting an error/warning "result of mkdirs() is ignored". Please help.

File path = new File(Environment.getExternalStorageDirectory() + "/My/Files");
    path.mkdirs();
    if(!path.exists()) { statusText.setText("Directories do not exist");}
    else recordingFile = File.createTempFile("MyRecording", ".pcm", path); 
Lefty
  • 53
  • 6
  • what is the error/ – Kaushal28 Apr 12 '17 at 17:20
  • " I keep getting an error/warning "result of mkdirs() is ignored"." -- you are welcome to ignore that if you want. Beyond that, you may need [runtime permissions](https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it) and you need to [index the file](https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl). – CommonsWare Apr 12 '17 at 18:03
  • Thank you @CommonsWare! I did not know about these permissions in SDK 23+, nor the file indexing. That's good advice! – Lefty Apr 12 '17 at 22:07

1 Answers1

1

Do you have the permission set in your manifest?

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

Also, Android Studio is giving you the warning about mkDirs () because it returns a boolean indicating whether the directory was created. It's just reminding you that you never used the result. It doesn't matter.

jburn2712
  • 167
  • 1
  • 7
  • Thanks for the answer. I did confirm that I already had the correct permission in my Manifest file to write to external storage. I was expecting to run this code, and then be able to actually find my new directories and file on File Explorer when I mount my phone to the laptop. No such luck. I figured mkdirs() would actually create these subfolders but it does not, or I'm missing something. – Lefty Apr 12 '17 at 21:43
  • I think I'm onto something, I hadn't realized. My app will stipulate minimum SDK of 25. I just read anything at SDK 23 and higher requires the explicit establishment of a user permissions method to actually enable the writing to external storage. – Lefty Apr 12 '17 at 21:48
  • Ah yes, you have to actually ask the permission at runtime in addition to declaring it in the manifest. I'm sure you're already looking at it, but [here's](https://developer.android.com/training/permissions/requesting.html) the Android developer page for implementing it. They show how to use compatibility methods to cover runtime permissions for all versions at once, including below 23. It helps to avoid version-checking in your code – jburn2712 Apr 13 '17 at 08:36