2014-03-28 40 views
0

我是一名新手Android开发人员。我已经使用通用图像加载器加载了一个图像,我想将它保存在我的SD卡上。该文件是在所需的目录中创建的,文件名正确,但文件大小始终为0.我做错了什么?使用Android通用图像加载器将图像保存为当前视图到SD卡

一个相关的片段如下:

PS:图像已经存在于磁盘,它没有被从互联网上下载。

private void saveImage(String imageUrls2, String de) { 
     String filepath = Environment.getExternalStorageDirectory() 
       .getAbsolutePath(); 
     File SDCardRoot = Environment.getExternalStorageDirectory() 
       .getAbsoluteFile();  
     String filename = de; 
     File myDir = new File(SDCardRoot+"/testdir"); 
     Bitmap mSaveBit = imageLoader.getMemoryCache(); 
     File imageFile = null; 

     try {   
      //create our directory if it does'nt exist 
      if (!myDir.exists()) 
       myDir.mkdirs(); 

      File file = new File(myDir, filename); 
      if (file.exists()) 
       file.delete(); 

      FileOutputStream fileOutputStream = new FileOutputStream(file); 
      BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 

      bos.flush(); 
      bos.close(); 

     } catch (IOException e) { 
      filepath = null; 
      e.printStackTrace(); 
      Toast.makeText(getApplicationContext(), 
        R.string.diskful_error_message, Toast.LENGTH_LONG) 
        .show(); 
     } 
     Log.i("filepath:", " " + filepath); 
    } 
+0

[从android通用图像加载器下载图像流]的可能重复(http://stackoverflow.com/questions/13855166/download-image-stream-from-android-universal-image-loader) – Emmanuel

回答

1

是的,你的代码只在sdcard_root/testdir/de上创建一个文件,并且没有写任何东西。 “imageUrls2”是源图像文件吗?如果是,您可以使用BufferedInputStream打开该文件,从BufferedInputStream中读取数据,并在bos.flush()和bos.close()之前使用bos.write()将其复制到输出文件。

希望它有帮助。