0

I have tried each and every solution on SO about writing to external storage. working on it from 2 days but unable to write in Android Kitkat 4.2.2. I am clueless whats happening. My manifest files have Read & Write permissions and code for writing file is :

// make a new file directory inside the "sdcard" folder
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "imgcaptureapp");

    // if the directory does not exist
    if (!mediaStorageDir.exists()) {
        // if you cannot make this directory return
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    // take the current timeStamp
    String timeStamp = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss").format(new Date());
    File mediaFile;
    // and make a media file:
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

    return mediaFile;

My mediaFile.canwrite(); returning false.

عثمان غني
  • 2,786
  • 4
  • 52
  • 79
  • 3
    Try not using colons in filenames. – CommonsWare Dec 11 '15 at 17:01
  • @CommonsWare Perfectly works. Awesome & thanks alot. – عثمان غني Dec 11 '15 at 17:03
  • 1
    Related: [What characters allowed in file names on Android?](https://stackoverflow.com/questions/2679699/what-characters-allowed-in-file-names-on-android). Unfortunately, the set of valid filename characters depends on type of the filesystem, the external storage directory is on. Hence: be conservative. – dhke Dec 11 '15 at 17:05
  • @dhke but I said that I am checking the permissions and trying many other methods but clueless about the filename problem. – عثمان غني Dec 11 '15 at 17:34

1 Answers1

1

Colons are reserved characters in many filesystems, including Android's. On the whole, the simpler the filename that you use, with respect to punctuation-style characters, the better off you will be.

In this case, use a date-time format (e.g., YYYYMMDD-HHMMSS) that happens to not use colons.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • You might want limit this a little. The `:` restriction only applies to the externally mounted sdcard, which usually has FAT32 as filesystem. Android is perfectly happy with colon elsewhere, e.g. on the emulated sdcard fs (usually ext4 or yaffs). – dhke Dec 12 '15 at 06:38