Alexandre Martin
Alexandre Martin

Reputation: 1502

How to create a new folder in android DCIM

I am actually able to capture a photo and to save it in android external storage DCIM folder.

My problem is that I can't create a new folder in it, so DCIM/MyPic.jpg became DCIM/MyFolder/MyPic.jpg.

Here is my source code :

File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "address");

if (!f.exists()) {
    f.mkdir();
}

File file = new File(Environment.getExternalStorageDirectory()
     + File.separator
     + "DCIM"
     + File.separator
     + "address"
     + File.separator
     , "IMG_001.jpg");

Notice that I correctly asked for WRITE_EXTERNAL_STORAGE permission in manifest.

The intent part to capture photo is fine, because I can save it directly to DCIM.

I do not get any error message, but nothing happens... no 'address' folder created :(

Thanks for help :D

Upvotes: 2

Views: 5858

Answers (1)

Tristan
Tristan

Reputation: 368

As posted in the comments, I tried your code and it worked for me.

MainActivity.java

public class MainActivity extends Activity {

    private final static String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "address");

        if (!f.exists()) {
            Log.d(TAG, "Folder doesn't exist, creating it...");
            boolean rv = f.mkdir();
            Log.d(TAG, "Folder creation " + ( rv ? "success" : "failed"));
        } else {
            Log.d(TAG, "Folder already exists.");
        }
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tristan.testcreatedirectory">

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

Before launching the app I have to enable the permission manually because I am on Android 6.

** Logs when launching the app the first time **

D/MainActivity: Folder doesn't exist, creating it...
D/MainActivity: Folder creation success

** Logs when launching the app the second time **

D/MainActivity: Folder already exists.

Upvotes: 9

Related Questions