0

I want to save some file in a subfolder of SDCard Documents folder. I've read many posts, finally I don't have any exception, it seems the file store is fine, but i still cannot see the folder, nor the files, when i connect the phone to the comp.

EDIT

this occurs only on the device, on emulator, the file is created

I've granted permission in manifest file

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

I've asked for write permission from the user, and it is granted. This post This post helped me in granting permission

here is my code

    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        Toast.makeText(_ctx, "There is no external storage", Toast.LENGTH_LONG).show();
    } else {
        File docfolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
        docfolder.mkdirs();
        if (!docfolder.exists()) {
            Toast.makeText(_ctx, "Documents doesn't exist", Toast.LENGTH_LONG).show();
        }
        File folder = new File(docfolder, foldername);
        folder.mkdirs(); //make if not exist

        boolean isPresent = folder.exists();
        if (isPresent) {
            File file = new File(folder.getAbsolutePath(), filename);

            try {
                OutputStream os = new FileOutputStream(file);
                os.write(xml.getBytes());
                os.close();
            } catch (IOException e) {
                Toast.makeText(_ctx, e.toString(), Toast.LENGTH_LONG).show();

                Log.w("ExternalStorage", "Error writing " + file, e);
            }
            catch (Exception e) {
                Toast.makeText(_ctx, e.toString(), Toast.LENGTH_LONG).show();

                Log.w("ExternalStorage", "Error writing 2 " + file, e);
            }
        } else {
            Toast.makeText(_ctx, "cannot make directory " + foldername, Toast.LENGTH_LONG).show();
            Log.w("ExternalStorage", "cannot make directory " + foldername);
        }

any hint is welcomed!

EDIT

maybe the issue could be that this code saves file into that path?

/storage/emulated/0/Documents/IBHTrack/ibhTrack_GPX_20170706153858.xml

however since i use the Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) method, i don't know how to get the real SDCard path

Community
  • 1
  • 1
pillesoft
  • 486
  • 1
  • 6
  • 20
  • https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl – CommonsWare Jul 06 '17 at 12:39
  • Possible duplicate of [How to write files to external public storage in Android so that they are visible from Windows?](https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl) – Sanoop Surendran Jul 06 '17 at 12:45
  • thanks, but is seems the mediascanner didn't help me. – pillesoft Jul 06 '17 at 13:18

0 Answers0