Below is the code to write to a file after downloading an image from the web. But somehow, it's just stuck while writing to File Stream. No Exception at all. It just stays on while loop then displays force close popup. it happens 2 or 3 out of 10.
private void saveImage(String fullPath) {
File imgFile = new File(fullPath);
try {
if(imgFile.exists()) imgFile.delete();
URL url = new URL(this.fileURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
InputStream in = httpConnection.getInputStream();
FileOutputStream fos = new FileOutputStream(imgFile);
int n = 0;
while((n = in.read()) != -1) {
fos.write(n);
}
fos.flush();
fos.close();
in.close();
}
catch(IOException e) {
imgFile.delete();
Log.d("IOException Thrown", e.toString());
}
}
When I check the file written, it's always stuck when it writes till 4576byte. (Original image size is over 150k) Is there anybody who can help me on this issue?