移动/重命名 SD 卡中的文件
我正在尝试将文件从一个目录移动到另一个目录(在 SD 卡中)
我有一个文件的 URI 以及我尝试移动它的方式:
Uri selectedImage = imageReturnedIntent.getData(); // this the uri, something like content://media/external/images/media/635
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard, selectedImage);
File to = new File(sdcard, "myNewDir/mynewfile.jpg");
from.renameTo(to);
但它不起作用,也没有在 Logcat 中给我任何错误。
编辑:
我已将这两种权限添加到我的清单文件中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I'm trying to move a file from one directory to another (in SD Card)
I have a file's URI and the way I'm trying to move it:
Uri selectedImage = imageReturnedIntent.getData(); // this the uri, something like content://media/external/images/media/635
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard, selectedImage);
File to = new File(sdcard, "myNewDir/mynewfile.jpg");
from.renameTo(to);
But it doesnt work, neither does it give me any error in Logcat...
Edit:
I have added both permissions to my manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
然后您要做的就是尝试将其连接到Environment.getExternalStorageDirectory() 上。这是行不通的。
content://media/external/images/media/635
既不是相对文件系统路径,也不是绝对文件系统路径。它是一个Uri
。如果您希望将图像从
Uri
复制到本地文件,请使用ContentResolver
获取InputStream
表示的图像上的InputStream
code>Uri,然后使用 Java I/O 将字节从InputStream
复制到目标文件。What you are then doing is trying to concatenate this onto
Environment.getExternalStorageDirectory()
. This will not work.content://media/external/images/media/635
is neither a relative filesystem path nor an absolute filesystem path. It is aUri
.If you wish to copy the image from the
Uri
to a local file, use aContentResolver
to get anInputStream
on the image represented by theUri
, then use Java I/O to copy the bytes from theInputStream
to your target file.