2011-11-30 96 views
1

在我的Android應用程序中,我想將媒體圖像複製到另一個文件夾(在我的下面的代碼中,我嘗試將圖片從「/mnt/sdcard/DCIM/Camera/my_photo.jpg」複製到「/ mnt/sdcard/PortFolio/MyGallery /。我試圖與下面的代碼,但它不工作。有人幫我出這???是有可能嗎?任何其他方式Android如何將媒體圖片移動到其他文件夾?

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.jpgsourceImagePathdestinationImagePath刪除/mnt/sdcard。你可能還需要createPortFolio/MyGallery文件夾第一。

啓動API級別8,也可以用它來獲取默認的圖片文件夾:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 

最後但並非最不重要的,請確保您have permission訪問SD卡。

相關問題