0

If I run the following code on an ASUS Transformer then 'testfile.txt' is created in the top level of the 'sd card' directory as expected.

private void testWriteFile () throws IOException
{
    File sdCard = Environment.getExternalStorageDirectory();
    File file = new File(sdCard, "testfile.txt");

    FileOutputStream myOutput = new FileOutputStream(file);
    byte buffer[] = {'0','1','2','3','4','5','6','7','8','9'};      

    myOutput.write(buffer,0,buffer.length);

    myOutput.flush();
    myOutput.close();
}

I can view the file on the ASUS Transformer using a File Manager app. I can see the file from a Windows dos box via the 'adb shell' command and its permissions appear to be set correctly. However, if I view the directory from Windows Explorer, 'testfile.txt' is not there. I have set the options to display hidden and system files, but with no luck.

If I use the ASUS file manager app to change the name of 'testfile.txt', then it becomes visible in Windows Explorer, so other apps are able to make files visible in Explorer.

If I run the same code on a ZTE Blade (Orange San Francisco) then I can see 'testfile.txt' in Windows Explorer without needing to change its name.

Is there something else I need to do to finalize this file so that it becomes visible?

A similar problem is reported here, Can't see a file in Windows written by an android app on sd-card unless I "Force Close" the app, but forcing a close in my case does not make the file visible.

Community
  • 1
  • 1
the wife
  • 61
  • 8

2 Answers2

5

I have solved this with the following instruction:

    MediaScannerConnection.scanFile
             (this, new String[] {file.toString()}, null, null);

I am not sure why this is required for the Asus and not for the ZTE Blade. However, there is a difference in the way that they connect to my PC and I suspect this is the reason.

The ZTE Blade appears as a USB mass storage device and is assigned a drive letter. The Asus appears as a portable media player (MTP) and is not assigned a drive letter.

the wife
  • 61
  • 8
0

I have this worked on android5.x:

File sdcard = Environment.getExternalStorageDirectory();
//step 1.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
              Uri.parse("file://" + sdcard)));

//step 2.
MediaScannerConnection.scanFile(getApplicationContext(), 
        new String[]{sdcard.getAbsolutePath() 
        + "/screenshots/myscreen_" + IMAGES_PRODUCED + ".png"}, null, null);

Thanks.

Eddy
  • 3,623
  • 37
  • 44