URLからSDカードに画像をダウンロードしました。SDカードでは通常のサムネイル画像として表示されますが、クリックして表示するとメッセージが表示されCould not load image
、画像はイメージビューで表示できません。このイメージは、非同期タスクの助けを借りてダウンロードされました。まず、画像が表示可能かどうかを確認したいと思います。そうでない場合は、削除して再ダウンロードしてください。
画像をダウンロードするためのコード:
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;
}
どんな助けでも事前に感謝します。