在我的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();
}
1 回答
sd
已包含/mnt/sdcard
. 你实际上是想打开/mnt/sdcard/mnt/sdcard/DCIM/Camera/my_photo.jpg
. 从sourceImagePath
和destinationImagePath
删除/mnt/sdcard
. 您可能还需要先createPortFolio/MyGallery
文件夹 .启动API级别8,您还可以使用它来获取默认图片文件夹:
最后但并非最不重要的是,请确保您have permission访问SD卡 .