Android: Files that I write to the sdcard do not s

2020-02-09 10:39发布

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?

标签: android
7条回答
爷、活的狠高调
2楼-- · 2020-02-09 11:13

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.

查看更多
Juvenile、少年°
3楼-- · 2020-02-09 11:13

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 ;-/

查看更多
够拽才男人
4楼-- · 2020-02-09 11:14

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.

查看更多
我只想做你的唯一
5楼-- · 2020-02-09 11:20

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/

查看更多
beautiful°
6楼-- · 2020-02-09 11:22

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 ?

查看更多
成全新的幸福
7楼-- · 2020-02-09 11:26

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");
    }
}

}

查看更多
登录 后发表回答