Android – how do I write files in an external SD card instead of in device storage?

I tried this:

  public String getFilename() {
    File file = new File(Environment.getExternalStorageDirectory(), "Test2");
    if (!file.exists()) {
        file.mkdirs();
    }
    String uriSting = (file.getAbsolutePath() + "/" + "IMG_" + System.currentTimeMillis() + ".png");
    return uriSting;
}

I tried to change the second line to:

File file = new File("sdcard/Test2");

However, the directory is still created in the device storage rather than on the external SD card

resolvent:

You do not have permission to access removable storage

On Android 4.4, you have two main choices:

>Use all methods on the context. Use getexternalfilesdirs(), getexternalcachedirs() or getexternalmediadirs() (please note the plural). If they return 2 locations, the second and subsequent locations are locations on removable storage, and your application can read and write without any permission. > use action_ OPEN_ DOCUMENT and ACTION_ CREATE_ Document, which allows users to choose where to place some content. Users can select removable storage (or Google cloud hard disk), Drop@R_628_2419 @Or external storage or anything else). You end up with a URI, not a file system path. Use contentresolver to work with that URI to get streams for reading and writing

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>