2

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

F43nd1r
  • 7,690
  • 3
  • 24
  • 62
Alexandre Martin
  • 1,472
  • 5
  • 14
  • 27

1 Answers1

9

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.
Tristan
  • 326
  • 2
  • 8
  • Thanks a lot for your answer, but the same code do not work for me :( I am unable to create the directory and then save an image in it. – Alexandre Martin Jun 01 '16 at 03:01
  • What are the logs when you run my code ? Can you paste it for me please ? – Tristan Jun 01 '16 at 03:03
  • D/MainActivity: Folder doesn't exist, creating it... D/MainActivity: Folder creation failed – Alexandre Martin Jun 01 '16 at 03:08
  • Ok are you able to put a breakpoint in the mkdir function ? (not the call, the implementation). There should be a try/catch into it, with a return false in the catch clause. Put a breakpoint on the return false; line and debug your app. You should be able to see the value of the exception and why it doesn't want to create the folder. – Tristan Jun 01 '16 at 03:12
  • I'll have a try tomorrow. Thanks for your time ! – Alexandre Martin Jun 01 '16 at 03:15
  • You were right ! My device is not on Marshmallow but I do target API 23 so requesting permission WRITE_EXTERNAL_STORAGE programmatically was the solution ! I will post my source code as answer for further needs, but you get the credit for it ! – Alexandre Martin Jun 01 '16 at 13:58