简体   繁体   中英

Android File not being copied to SD card

I'm trying to copy a file that is located in the External storage directory into a directory that is in my SD Card. However, when I check to see if the file has successfully been copied, the file is not even created in the SD Card.

Am I missing something? Here is the code I have:

String sourcePath = Environment.getExternalStorageDirectory().getPath() + newFileName;

File source = new File(Environment.getExternalStorageDirectory().getPath(), newFileName);
String destinationPath = "/storage/external_SD";
File destination = new File(destinationPath, newFileName);
try {
    if(!destination.exists()){
        destination.mkdir();
    }
    FileUtils.copyFile(source, destination);
} catch (IOException e) {
    e.printStackTrace();
}

The copyFile method is from an Apache library. Here is the link for it: https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html

However, when I check to see if the file has successfully been copied, the file is not even created in the sd Card.

You do not have arbitrary filesystem-level access to removable storage on Android 4.4+.

Is there a work around for this?

That depends on what your objective is.

If you insist that you must be able to write to that specific path on arbitrary user devices... then, no, there is no supported workaround. After all, there is no /storage/external_SD on the vast majority of Android devices. Where and how device manufacturers choose to mount removable media is up to them and is an implementation detail that will vary.

If you relax that restriction, but insist that you must be able to write a file to the root directory of removable storage on arbitrary user devices... then, no, there is no supported workaround today. The N Developer Preview has a "Scoped Directory Access" feature that should allow this, but it will be several years before you can assume that an arbitrary user device will be running that version of Android or higher. Also, you do not get actual filesystem access, but rather a Uri (see the Storage Access Framework option, below).

Now, if you are more flexible about the precise location, you have other options:

  • You can use getExternalFilesDirs() , getExternalCacheDirs() , and getExternalMediaDirs() , all methods on Context . Note the plural form. If those return 2+ entries, the second and subsequent ones are locations on removable storage that you can read from and write to, no permissions required. However, you do not get to choose the exact path. And if the device has 2+ removable storage volumes, I'm not quite certain how you would help the user tell them apart.

  • You can use the Storage Access Framework and let the user choose where to put the file. The user is welcome to choose removable storage... or not. You get a Uri back, which you can use with ContentResolver and openOutputStream() to write your content. You can also take persistable Uri permissions so you can work with that file again in the future, assuming the user doesn't move or delete it behind your back.

The destinationPath you mentioned may not be accessible as it may belong to the private system folders or some other application folders. You can however use public folders like Pictures,Music, Videos,Downloads,etc. or create sub folders inside them -

String sourcePath = Environment.getExternalStorageDirectory().getPath() + newFileName;

    File source = new File(Environment.getExternalStorageDirectory().getPath(), newFileName);
File destinationPath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS, "/external_SD");    

  try {
        if(!destinationPath.exists()){
            destinationPath.mkdir();
        }
File destination = new File(destinationPath, newFileName);
        FileUtils.copyFile(source, destination);
    } catch (IOException e) {
        e.printStackTrace();
    }

如果要复制到外部存储,则需要

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM