1

I am developing an android application using Android Studio and Java.

I need to create a directory and manipulate some files. But above all, I need this directory to be visible to the user when they connect the phone with a USB cable.

I am using the "external" memory of the phone. Not the internal one. Not even removable.

I am using Bookan code (Can't create folder on external storage on android) to create the directory.

private void createFolder () {
    if (isStoragePermissionGranted ()) {
        File folder = new File (Environment.getExternalStorageDirectory () + File.separator + "NewFolderCreated");

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

public boolean isStoragePermissionGranted () {
    if (Build.VERSION.SDK_INT> = 23) {
        if (checkSelfPermission (android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            ActivityCompat.requestPermissions (this,
                    new String [] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else {// permission is automatically granted on sdk <23 upon installation
        return true;
    }
}

In the manifest file, permissions are present.

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

    

Although WRITE's permission implies reading and writing, I have made both explicit.

When I have console output displayed for the path created, I have the answer:

/storage/emulated/0/NewFolderCreated

However, when accessing the directory through Windows Explorer, "NewFolderCreated" is neither a folder nor a file. Get that unassociated item icon in windows. You can see that windows does not know that it is a directory.

What am I doing wrong?

If I try to access the directory through android studio's file explorer, I have the permission error, which I can't solve either.

ls: /storage/emulated/: Perm

EDIT1: Added a screenshot

Screenshot

  • Note that none of this will work on Android 10, as [you do not have access to that directory](https://commonsware.com/blog/2019/06/07/death-external-storage-end-saga.html). – CommonsWare Sep 14 '19 at 13:45
  • I had done some testing with MediaScanner, but I removed it from implementation on this question. My problem seems to be different since I can create and display "something" in windows explorer. However, windows does not know that this "something" is a directory. The icon used by the system is of something unknown, without association. – Juliano Rodrigo Lamb Sep 15 '19 at 22:48
  • That feels like you tried telling the `MediaScannerConnection` to scan a directory. Tell it to scan a file, after you create the file in the directory. – CommonsWare Sep 15 '19 at 22:59
  • That is the correct answer. My question is not duplicated. I cannot scan a directory, I must scan a file. I am still solving the fact that the created file cannot be read and for that I am reading about FileOutputStream and InputStream; But that's another story. Thank you @CommonsWare – Juliano Rodrigo Lamb Sep 16 '19 at 16:22

0 Answers0