I am prepared code for download images from server and store in sdcard.For that i have prepared code,
URL url;
try {
url = new URL("http://myserver_path/applikaza/appstore/apks/ScrResoluton/scrresolution.jpg");
input = url.openStream();
String storagePath = Environment.getExternalStorageDirectory().toString();
String basepath = storagePath + "/Guayama/" + folderName;
output = new FileOutputStream(basepath + "/home.png");
byte[] buffer = new byte[5000];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} catch (MalformedURLException e) { // TODO Auto-generated catch block
e.printStackTrace();
} finally {
output.close();
input.close();
}
but got NullPointerException at output.close().I think done mistake some where .please help me.
The reason you get a NullPointerException at output.close() is because your URL is incorrectly formed. For starters, it doesn’t contain a valid protocol. This will cause MalformedURLException to be thrown on the line
and you’ll then go straight to the catch block, followed by the finally block which calls output.close(), but output is null since the line
was never executed.
You need to correct your URL (see the description here) and you need to correct your exception handling, as in the case of MalformedURLException, this will only ever be thrown where you set the value of url, and so you will never have a non-null value for output or input when your “finally” block is executed.
Your URL should probably be of the form “http://something/Suresh/images/home.png” or maybe “file://something/Suresh/images/home.png”. If the home.png file is located on another machine to the Android device, try and access it via a web browser and use the URL shown by the web browser complete with “http://”.