Android如何将媒体图片移动到另一个文件夹?
在我的android应用程序中,我想将媒体图像复制到另一个文件夹(在下面的代码中,我尝试将图片从“/mnt/sdcard/DCIM/Camera/my_photo.jpg”复制到“/mnt/sdcard/PortFolio/MyGallery/” .我用下面的代码尝试了这个,但它不起作用?有人帮助我吗?还有其他方法吗?
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String sourceImagePath= "/mnt/sdcard/DCIM/Camera/my_photo.jpg";
String destinationImagePath= "/mnt/sdcard/PortFolio/MyGallery/";
Log.d("destinationImagePath", ""+destinationImagePath);
File source= new File(data, sourceImagePath);
File destination= new File(sd, destinationImagePath);
Log.d("before copying", "");
if (source.exists()) {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
In my android application i want to copy the media images to another folder(in my below code i try to copy picture from "/mnt/sdcard/DCIM/Camera/my_photo.jpg" to "/mnt/sdcard/PortFolio/MyGallery/ . I tried this with the following code but it doesn't work. Someone help me out of this??? Is there any other way possible??
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String sourceImagePath= "/mnt/sdcard/DCIM/Camera/my_photo.jpg";
String destinationImagePath= "/mnt/sdcard/PortFolio/MyGallery/";
Log.d("destinationImagePath", ""+destinationImagePath);
File source= new File(data, sourceImagePath);
File destination= new File(sd, destinationImagePath);
Log.d("before copying", "");
if (source.exists()) {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sd
已包含/mnt/sdcard
。您实际上正在尝试打开/mnt/sdcard/mnt/sdcard/DCIM/Camera/my_photo.jpg
。从sourceImagePath
和destinationImagePath
中删除/mnt/sdcard
。您可能还需要创建PortFolio首先是 /MyGallery
文件夹。从 API 级别 8 开始,您还可以使用它来获取默认图片文件夹:
最后但并非最不重要的一点是,请确保您 有权限访问SD卡。
sd
already contains/mnt/sdcard
. You are actually trying to open/mnt/sdcard/mnt/sdcard/DCIM/Camera/my_photo.jpg
. Remove/mnt/sdcard
fromsourceImagePath
anddestinationImagePath
. You would probably also need to create thePortFolio/MyGallery
folder first.Starting API level 8, you can also use this to get the default pictures folder:
And last but not least, make sure you have permission to access the SD card.