我在Android中下载图片网址时出现问题,我有一个Facebook相册图片网址,当我尝试将图片下载到移动存储时,我收到了Exception FileNotFound。代码如下。任何人有解决方案请帮助我。 感谢Advance。请不要评价我。
protected Void doInBackground(String... strings) {
try
{
URL url = new URL(photos.get(1).getPictureUrl());
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), "Image");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
mediaStorageDir = new File("storage/emmc"+File.separator);
}
}
String filename="downloadedFile.png";
Log.i("Local filename:",""+filename);
File file = new File(mediaStorageDir,filename);
if(file.createNewFile())
{
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 null;
}