SDCard中的图像下载

时间:2015-02-16 18:58:46

标签: android

我从sdcard中的url下载图片,但它返回FileNotFound Exception但是它在浏览器上显示图像。我不知道为什么?请建议我任何解决方案。

代码:

public String getImageDownloaded(String url){
        String filepath = "";
        try
        {   
        //String str = url.substring(0, 7);

         URL urlImage = new URL(url);
         //URL urlImage = new URL("http://staging.okay-app.com/system/album_images/images/000/000/123/original/IMG-20150209-WA0012.jpg");
          HttpURLConnection urlConnection = (HttpURLConnection) urlImage.openConnection();
          urlConnection.setRequestMethod("GET");
          urlConnection.setDoOutput(true);                   
          urlConnection.connect();                  
          File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile();
          String filename="okay_profile_pic.png";   
          Log.i("Local filename:",""+filename);
          File file = new File(SDCardRoot,filename);

            file.createNewFile();

          FileOutputStream fileOutput = new FileOutputStream(file);
          InputStream inputStream = urlConnection.getInputStream();
          int totalSize = urlConnection.getContentLength();
          int downloadedSize = 0;   
          byte[] buffer = new byte[1024];
          int bufferLength = 0;
          while ( (bufferLength = inputStream.read(buffer)) > 0 ) 
          {                 
            fileOutput.write(buffer, 0, bufferLength);                  
            downloadedSize += bufferLength;                 
            Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
          }             
          fileOutput.close();
          if(downloadedSize==totalSize) filepath=file.getPath();    
        } 
        catch (MalformedURLException e) 
        {
          e.printStackTrace();
        } 
        catch (IOException e)
        {
          filepath=null;
          e.printStackTrace();
        }
        Log.i("filepath:"," "+filepath) ;
        return filepath;
    }

提前致谢。

修改

我通过删除urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true);解决了这个问题,因为当我想使用GET时,setDoOutput(true)强制为POST方法。谢谢大家的支持。

1 个答案:

答案 0 :(得分:0)

您需要为我发布更多信息才能跟踪此信息。但是现在进行这些更改并让我知道会发生什么。

//file.createNewFile();

// Make this OutputStream not FileOutputStream
OutputStream fileOutput = new FileOutputStream(file);
int totalSize = urlConnection.getContentLength();
          int downloadedSize = 0;   
          byte[] buffer = new byte[1024];
          int bufferLength = 0;
          while ( (bufferLength = inputStream.read(buffer)) != -1 ) 
          {                 
            fileOutput.write(buffer, 0, bufferLength);                  
            downloadedSize += bufferLength;                 
            Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
          }
          fileOutput.flush();             
          fileOutput.close();
          inputStream.close();
          if(downloadedSize==totalSize)
           filepath=file.getPath();    
        }