【问题标题】:copy File form external Storage folder into the same folder with different file extension将文件从外部存储文件夹复制到具有不同文件扩展名的同一文件夹中
【发布时间】:2021-03-18 15:02:15
【问题描述】:

我想将文件从外部存储复制到同一个文件夹,但文件扩展名不同。 它适用于内部 SD 卡:

作品:/storage/emulated/0/Music/Test.txt -> /storage/emulated/0/Music/Test.log

不起作用:/storage/3339-6133/Test/Test.txt -> /storage/3339-6133/Test/Test.log

设置的权限,好像不是这个问题。

Log.d(TAG, "copyRename...path 1: "+path);
path = path.substring(0,path.lastIndexOf("/")+1);
Log.d(TAG, "copyRename...path 2: "+path);
String destName = name.substring(0,name.length()-4)+".log";
Log.d(TAG, "copyRename...destName: "+destName);

File from = new File(path,name);
File to = new File(path,destName);

boolean rC = from.renameTo(to);
Log.d(TAG, "copyRename...rename done: "+rC);

内部和外部的Logcat:

MainActivity: copyRename...name: Test.txt
MainActivity: copyRename...path 1: /storage/emulated/0/Music/Test.txt
MainActivity: copyRename...path 2: /storage/emulated/0/Music/
MainActivity: copyRename...destName: Test.log
MainActivity: copyRename...rename done: true

MainActivity: copyRename...name: Test.txt
MainActivity: copyRename...path 1: /storage/3339-6133/Test/Test.txt
MainActivity: copyRename...path 2: /storage/3339-6133/Test/
MainActivity: copyRename...destName: Test.log
MainActivity: copyRename...rename done: false

谁能给我提示一下怎么办?

谢谢, 亚历杭德罗

【问题讨论】:

  • 自 Android Kitkat 以来,可移动 micro sd 卡是只读的。您只能完全访问其中的一个应用特定目录。
  • 所以把你的文件放在 Windows 下的那个应用程序的特定目录中。对于路径,请查看 getExternalFilesDirs() 返回的第二项。
  • @blackapps:就是这样。非常感谢。

标签: android copy rename external


【解决方案1】:

您是否有权写入此文件夹/storage/3339-6133//emulated/0/是外置存储卡,WRITE_EXTERNAL_STORAGE这个路径就足够了,但是这个/storage/3339-6133/Test/文件夹好像在系统权限的某个地方,一般的应用程序永远无法访问这个路径

【讨论】:

  • WRITE_EXTERNAL_STORAGE 已设置。我使用 Windows 资源管理器复制此文件夹中的文件,并使用 ContentProvider Query 获取路径。尝试从文件夹/storage/3339-6133/Storage Card/Test/ 复制时得到相同的结果。此文件夹是外部设备存储卡。
猜你喜欢
  • 1970-01-01
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 2014-11-08
  • 1970-01-01
相关资源
最近更新 更多