我已将图像从 url 下载到 sd 卡。它在 sd 卡中显示为普通缩略图,但是当我单击它查看它时,Could not load image
还会显示一条消息,该图像在 imageview 中不显示。该图像是在 Async Task 的帮助下下载的。现在我想先检查图像是否可显示。如果没有删除它并重新下载它。
下载图片的代码:
protected Void doInBackground(String... params) {
try{
String fileName = params[0].substring(params[0].lastIndexOf('/') + 1);
URL url = new URL(params[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File file = new File(SwipeActivity.filename,fileName);
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 )
{
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
}
catch (Exception e) {
// TODO: handle exception
}
return null;
}
提前感谢任何帮助。