简体   繁体   English

Android文件未复制到SD卡

[英]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. 我正在尝试将外部存储目录中的文件复制到SD卡中的目录中。 However, when I check to see if the file has successfully been copied, the file is not even created in the SD Card. 但是,当我检查文件是否已成功复制时,甚至没有在SD卡中创建文件。

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. copyFile方法来自Apache库。 Here is the link for it: https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html 这是它的链接: 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. 但是,当我检查文件是否已成功复制时,该文件甚至没有在sd卡中创建。

You do not have arbitrary filesystem-level access to removable storage on Android 4.4+. 您无法在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. 毕竟,绝大多数Android设备上都没有/storage/external_SD 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. N Developer Preview具有“作用域目录访问”功能,该功能应允许此功能,但是要假定任意用户设备将运行该版本的Android或更高版本还需要几年的时间。 Also, you do not get actual filesystem access, but rather a Uri (see the Storage Access Framework option, below). 另外,您没有获得实际的文件系统访问权限,而是获得了Uri (请参阅下面的“存储访问框架”选项)。

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 . 您可以使用Context上的所有方法getExternalFilesDirs()getExternalCacheDirs()getExternalMediaDirs() 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. 如果这些返回2+条目,则第二个及后续条目是可移动存储上的可读取和写入的位置,无需权限。 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. 而且,如果该设备具有2个以上的可移动存储卷,则我不确定您如何帮助用户区分它们。

  • You can use the Storage Access Framework and let the user choose where to put the file. 您可以使用Storage Access Framework并让用户选择将文件放置在何处。 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. 您将获得一个Uri ,可以将其与ContentResolveropenOutputStream()来编写内容。 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. 您还可以获取持久的Uri权限,以便将来假定用户不会将其移动或删除时再使用该文件。

The destinationPath you mentioned may not be accessible as it may belong to the private system folders or some other application folders. 您提到的destinationPath可能属于私有系统文件夹或某些其他应用程序文件夹,因此可能无法访问。 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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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