0

I'm trying to make it so when a user launches the app for the first time, a new folder is created within the Gallery section of the phone and ISN'T duplicated every time the app is launched. I've looked through some answers on here but most are either confusing or out-of-date. Please can somebody help?

I tried the following but it doesn't seem to work:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_profile); final File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "MyNewFolderName" + File.separator) }; 

I have also included this line in AndroidManifext.xml:

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

7
  • this issue is in Android 6? Commented Mar 10, 2016 at 17:02
  • I'm testing on 5.1.1 Commented Mar 10, 2016 at 17:02
  • 1
    Beyond Budius' answer, note that the directory itself may not show up in file explorers for some time, particularly while the directory is empty, due to the way external storage works. However, the directory will exist, and you can start putting images there. Commented Mar 10, 2016 at 17:03
  • @CommonsWare - is there a quick way to find out that it has created it without looking in the gallery? Commented Mar 10, 2016 at 18:13
  • @blueprintChris: The file browser in the Android Device Monitor should show the directory immediately (get to that from Tools > Android from the Android Studio main menu). Or, use adb shell to get at the command line shell in the device or emulator and poke around external storage that way. Both of those work off the filesystem, not the MediaStore, and so your new directory should be visible immediately. Commented Mar 10, 2016 at 18:20

1 Answer 1

4

you were so so close to the answer:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_profile); // this line creates reference to a folder that might or not exist, but does not create File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "MyNewFolderName" + File.separator) root.mkdirs(); // this line creates the folder }; 

also, if you're running that on Marshmallow (android 6), you have to first ask permission to write to storage, you can read here about asking permission http://developer.android.com/training/permissions/index.html

Sign up to request clarification or add additional context in comments.

1 Comment

I'm assuming that this will only create it once and only once?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.