SD card data sometimes NOT seen on PC when connect

2019-09-09 19:47发布

问题:

Possible Duplicate:
SDCard content exist but cant see them

Phone running Linux Kernel(2.6.31) with SD card inserted.

[ISSUE] When data is written to SD card (write syscall) the write returns successfully. However, when the card (via phone) is accessed as a mass storage device on PC, the data is not seen.

Only after physically removing the card and reinserting it and then accessing it on PC as a mass storage device will show the data that was written. Tried fsync() after writing the data. Still it wont show.

Is the kernel maintaining a cache before writing the data to SD card? If so, how do I make sure it is flushed to the SD card?

[edit] removed tag MMC

回答1:

Your problem is an exact duplicate of SDCard content exist but cant see them - the PC is displaying the content as returned from the MTP interface.

Stub code to do what you need to make the file appear:

imports:

import android.media.MediaScannerConnection;
import android.os.Environment;
import android.util.Log;
import java.io.File;

stub code:

File f = new File(Environment.getExternalStorageDirectory().getPath() + "/hello_nurse.txt");
if (! f.exists()) {
    try {
        f.createNewFile();
        String[] files = new String[1];
        files[0] = Environment.getExternalStorageDirectory().getPath() + "/hello_nurse.txt";
        String[] mimes = new String[1];
        mimes[0] = "text/plain";
        MediaScannerConnection.scanFile(getApplicationContext(), files, mimes, null);
    } catch (Exception ex) {
        Log.e("SD Create", "Failed to create file", ex);
        return;
    }
} else {
    Log.e("SD Create", "File is already present");
}