5

This is the code i am using to create a folder in the default pictures folder:

File imagesFolder = new File(Environment.DIRECTORY_PICTURES, "/images");
if (!imagesFolder.exists()) {
Log.d("if imagesFolder exists - 1", "False");
imagesFolder.mkdirs();
} else {
Log.d("if imagesFolder exists - 1", "True");
}
if (!imagesFolder.exists()) {
Log.d("if imagesFolder exists - 2", "False");
imagesFolder.mkdirs();
} else {
Log.d("if imagesFolder exists - 2", "True");
}

in log i am getting:

False

False

for the 1st time the directory is not present, hence False but then immediately i am creating it using mkdirs(), hence i expect the 2nd log to be True but even that is False and my application crashed because of NullPointerException in the later part of the code

Please help

Thank You

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

2 Answers2

11

You are using Environment.DIRECTORY_PICTURES the wrong way. It's just a String constant like "Pictures" but not a path. You need to get the path via Environment.getExternalStoragePublicDirectory(string)

File pictureFolder = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES
        );
File imagesFolder = new File(pictureFolder, "images");
// etc
zapl
  • 63,179
  • 10
  • 123
  • 154
  • @zapi 1 problem here is when i am saving the images in this folder, the size of the images are 30-50 K.B(I am not compressing) where as the size of normal images (taken from camera from home screen) are 2-3 M.B sized. Do you have any idea why it is so? – Archie.bpgc Sep 26 '12 at 10:35
  • @Archie.bpgc You probably save images with a lower resolution, scaled down somewhere. Has definitely nothing to do with the place you safe the images. – zapl Sep 26 '12 at 11:44
  • Scaling i am definitely not doing anywhere in my code. But i am using this: "bitmap.compress(Bitmap.CompressFormat.PNG, 100, bao);" But a PNG is lossless format right? moreover i am using 100 as the quality. Are there any other things to look into? – Archie.bpgc Sep 26 '12 at 12:37
  • @Archie.bpgc whatever you save there has less "image information" ([Entropy](http://en.wikipedia.org/wiki/Entropy_%28information_theory%29) if you like it abstract) than the camera images. Either smaller in dimensions or in image content (less details, colors, etc). Yes, PNG is lossless but can use compression. I still think your images have smaller dimensions, than the several megapixel ones from the camer. – zapl Sep 26 '12 at 12:57
1

To generate a folder at first you need to add permission at AndroidMinifest.xml

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

Now call following method to create a new folder with in which Directory you want to create your folder(this name should exist in Environment. list) and your folder name.

File outputDirectory = GetPhotoDirectory(Environment.DIRECTORY_PICTURES, "YourFolder");

Generate your folder by this method

public static File GetDirectory(String inWhichFolder, String yourFolderName ) {
    File outputDirectory = null;

    String externalStorageState = Environment.getExternalStorageState();
    if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {

        File pictureDirectory = Environment.getExternalStoragePublicDirectory(inWhichFolder);

        outputDirectory = new File(pictureDirectory, yourFolderName);
        if (!outputDirectory.exists()) {
            if (!outputDirectory.mkdirs()) {
                Log.e(LogHelper.LogTag, "Failed to create directory: " + outputDirectory.getAbsolutePath());
                outputDirectory = null;
            }
        }
    }
    return outputDirectory;
}

If you want to create a file under your newly created folder then you can use below code

public static Uri GenerateTimeStampPhotoFileUri(File outputDirectory, String fileName){
    Uri photoFileUri = null;

    if(outputDirectory!=null) {
        File photoFile = new File(outputDirectory, fileName);
        photoFileUri = Uri.fromFile(photoFile);
    }
    return photoFileUri;
}

Call to create a file after creating the folder with File Directory. It will return your file Uri

Uri fileUri = GenerateTimeStampPhotoFileUri(outputDirectory, fileName);
underflowed
  • 69
  • 1
  • 9
reza.cse08
  • 5,938
  • 48
  • 39