11

In my application, I create files and write them to the Sdcard. My created files show properly in DDMS, and in windows explorer when I mount my Samsung Galaxy as a USB device. But, they do not show in Windows explorer on my Acer Iconia Tab A500, when it is connected by USB. I assume this is due to some difference in 3.0? Do I need to create my files differently in 3.0 so that they show in Windows by usb?

ab11
  • 19,770
  • 42
  • 120
  • 207
  • doesn't work regardless of location. I've tried in a few sub directories of /mt/sdcard/ – ab11 Jun 24 '11 at 14:00
  • since windows hides some system directories, so I asked? – Neeraj Nama Jun 24 '11 at 14:01
  • fair enough, but I don't think that is the issue. If I use windows explorer to drag a file to /mnt/sdcard/, unplug the device, and plug it back in; the file I added through explorer shows up in the expected location. However, if I create file through code, and write it to /mnt/sdcard/, it does not show up in the windows explorer (though it does show in ddms, and works fine on my 2.2 devices). – ab11 Jun 24 '11 at 14:11
  • To be clear, are you mounting the USB drive when you connect the tablet to your Windows machine or simply connecting via USB? – Pedantic Jun 24 '11 at 14:12
  • for the galaxy, I am mounting it. for the Acer, I am not. It seems that it doesn't "mount", it just shows up as a device when I connect it to windows (but the tablet is still completely usable while connected). – ab11 Jun 24 '11 at 14:16
  • are they visible on any linux platform? – Neeraj Nama Jun 24 '11 at 14:18

7 Answers7

10

Sending this intent after writing the file solved the issue. Credit goes to Commons Guy, he provided the answer on another forum.

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
            + Environment.getExternalStorageDirectory()))); 

Edit: Commons Guy did comment that sending a fake intent (like above) is probably a bad idea, so I used this instead:

   //called after writing file, from my activity 
    new SingleMediaScanner(this, path); 

    private class SingleMediaScanner implements MediaScannerConnectionClient 
    { 
            private MediaScannerConnection mMs; 
            private String path; 
            SingleMediaScanner(Context context, String f) 
            { 
                mPath = f; 
                mMs = new MediaScannerConnection(context, this); 
                mMs.connect(); 
            } 
            @Override 
            public void onMediaScannerConnected() 
            { 
                mMs.scanFile(mFile, null); 
            } 
            @Override 
            public void onScanCompleted(String path, Uri uri) 
            { 
                mMs.disconnect(); 
            } 
        }
ab11
  • 19,770
  • 42
  • 120
  • 207
  • I found [the full discussion on Google Groups](https://groups.google.com/forum/#!topic/android-developers/FSi2iMHpD88), which was enlightening as well. I notice you have, variously, path, mPath and mFile within your class... are these all supposed to be the same "private String path" variable, or is something else absent? I'm going on the assumption that these are just typos. – MartyMacGyver Oct 13 '11 at 00:46
  • Interestingly you should never try running this against a folder... I'm trying to find a way to make new (but empty) folders appear to the user. May have to just create it and an empty file, scan it, then delete the empty file. Otherwise Windows sees that newly scanned folder as an unreadable *file*. – MartyMacGyver Oct 13 '11 at 02:07
  • I had the same issue on an Xperia running 4.03. Passing the created file to the media scanner service in this way solved the problem and now also Windows Explorer sees it. I did not realize at all that the files are apparently queried from a service instead of something more direct. – Pasi Välkkynen Jun 07 '12 at 11:07
5

On my Acer Iconia Tab, getExternalStorageDirectory actually points to an internal storage (considered as "shared", as opposed to the "private" inner storage), and is mounted as /mnt/sdcard. There is an alternative (and "true") external storage that goes on the SDcard at /mnt/external_sd. I'm not sure if there currently is a perfect method to get that path, though. Do you know which partition is mounted when you connect your Iconia Tab by USB ?

Gregory
  • 4,384
  • 1
  • 25
  • 21
  • the same happens with Galaxy S2. – renam.antunes Jul 04 '11 at 20:46
  • Thanks for the answer, sorry for the delayed response... Yes, on my Acer getExternalStorageDirectory() gives "/mnt/sdcard". The device has a directory "/mnt/external_sd" but it appears to be empty when I browse into it with ddms, and I get a "permission denied" exception when I try to write a file to this directory. I'm not sure what partition is mounted when I connect by USB, any idea how I could find out? – ab11 Jul 06 '11 at 12:15
  • 2
    You could just try to copy a file on the mounted partition from your PC, and then use a file explorer on Android to see where that file went. – Gregory Jul 06 '11 at 12:25
4

Also found this problem on a Nexus 7. If you device connects to your PC with MTP (the preferred USB connection protocol post honeycomb), when you create a file, the media scanner needs to be updated for it to be immediately visible in Windows Explorer.

Some devices update the media scanner automatically (Samsung S4), some do not (Nexus 7). After creating the new file on the SD card, to make sure the media scanner is updated make the following call.

File file= new File(FullPathAndFileNameToYourNewFile);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);

Here is a really good post on the topic. Thanks go to the author for the explanation and code above. https://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/

Marky0
  • 1,984
  • 2
  • 12
  • 28
0

Thought I'd post a new answer, because the current accepted answer doesn't work for anything after KitKat.

File imageFile = ....

MediaScannerConnection.scanFile(this, new String[] { imageFile.getPath() }, new String[] { "image/jpeg" }, null);

Solution provided by darrenp here.

Community
  • 1
  • 1
emhomm4
  • 212
  • 1
  • 9
0

This may be from way out in left field. I am new to Android developing and was having the same problem: I save a file to the sd card, and I don't even SEE the ANDROID directory when I hook up my phone to my windows computer to look at the phone. On a whim, I went to Developer options (that thing you get by tapping the build number 7 times) to configure developer options and I turned "Developer options" off. I think somewhere in the mayhem I toggled "USB debugging" on and off as well. Had to do the seven taps thing again. Now I see the ANDROID directory and the files I saved under ANDROID/data/com.blahblah.blahblah/files. Go figure. Maybe a version of "reboot." Random, I know, but it worked for me ;-/

0

example of create read and write file sdcard.

public class ReadWriteSDCardFile extends Activity {

private static final String LOGTAG = "FileStorage";

private TextView readOutput;

@Override
public void onCreate(final Bundle icicle) {
    super.onCreate(icicle);
    this.setContentView(R.layout.read_write_sdcard_file);

    this.readOutput = (TextView) findViewById(R.id.readwritesd_output);

    String fileName = "testfile-" + System.currentTimeMillis() + ".txt";

    // create structure /sdcard/unlocking_android and then WRITE
    File sdDir = new File("/sdcard/");
    if (sdDir.exists() && sdDir.canWrite()) {
        File uadDir = new File(sdDir.getAbsolutePath() + "/unlocking_android");
        uadDir.mkdir();
        if (uadDir.exists() && uadDir.canWrite()) {
            File file = new File(uadDir.getAbsolutePath() + "/" + fileName);
            try {
                file.createNewFile();
            } catch (IOException e) {
                Log.e(ReadWriteSDCardFile.LOGTAG, "error creating file", e);
            }

            // now that we have the structure we want, write to the file
            if (file.exists() && file.canWrite()) {
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);
                    fos.write("I fear you speak upon the rack, where men enforced do speak anything.".getBytes());
                } catch (FileNotFoundException e) {
                    Log.e(ReadWriteSDCardFile.LOGTAG, "ERROR", e);
                } catch (IOException e) {
                    Log.e(ReadWriteSDCardFile.LOGTAG, "ERROR", e);
                } finally {
                    if (fos != null) {
                        try {
                            fos.flush();
                            fos.close();
                        } catch (IOException e) {
                            // swallow
                        }
                    }
                }
            } else {
                Log.e(ReadWriteSDCardFile.LOGTAG, "error writing to file");
            }

        } else {
            Log.e(ReadWriteSDCardFile.LOGTAG, "ERROR, unable to write to /sdcard/unlocking_android");
        }
    } else {
        Log
            .e(
                ReadWriteSDCardFile.LOGTAG,
                "ERROR, /sdcard path not available "
                    + "(did you create an SD image with the mksdcard tool, and start emulator with -sdcard <path_to_file> option?");
    }

    // READ
    File rFile = new File("/sdcard/unlocking_android/" + fileName);
    if (rFile.exists() && rFile.canRead()) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(rFile);
            byte[] reader = new byte[fis.available()];
            while (fis.read(reader) != -1) {
            }
            this.readOutput.setText(new String(reader));
        } catch (IOException e) {
            Log.e(ReadWriteSDCardFile.LOGTAG, e.getMessage(), e);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // swallow
                }
            }
        }
    } else {
        this.readOutput.setText("Unable to read/write sdcard file, see logcat output");
    }
}

}

rabby
  • 335
  • 2
  • 2
  • Sorry if I wasn't clear in my question. I know how to write to the sdcard; when I write to the Sdcard, everything works as expected for my Samsung Galaxy 2.2, but not for my Acer AconiaTab 3.0. For the AconiaTab, my created files do not show in Windows explorer, when the device is connected by USB. – ab11 Jun 24 '11 at 14:09
0

How are you determining the location of the SD Card?

It sounds like you are assuming the SD Card is always mounted at /mnt/sdcard. You should absolutely never hardcode paths in Android since each implementation may mount the SD card in a different location. Rather, you should be using getExternalStorageDirectory() to determine where the SD card is mounted, and build your path from that.

RivieraKid
  • 5,923
  • 4
  • 38
  • 47