I am trying to get image from URL to ImageView I’m curently using this class. I have added permission to connect to internet write_SD card and so on, to AndroidManifest the problem is that it returns error java.lang.NullPointerException when I try to add image to ImageView using this function:`
public void download(String url, ImageView imageView) {
try
{
if (cancelPotentialDownload(url, imageView))
{
//TODO:Caching code right here
String filename = String.valueOf(url.hashCode());
File f = new File(getCacheDirectory(hm), filename);
// Is the bitmap in our cache?
Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
//No? download it
if(bitmap == null){
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
imageView.setImageDrawable(downloadedDrawable);
task.execute(url);
}else{
//Yes? set the image
imageView.setImageBitmap(bitmap);
}
}
}catch (Exception e) {
// TODO: handle exception
Log.e("",e.toString());
}
}`
The question is what could be wrong and how could i fix this?
i will be pleased if you could point me in the right direction.
Logcat:
`06-06 09:38:19.686: E/AndroidRuntime(18832): java.lang.NullPointerException
06-06 09:38:19.686: E/AndroidRuntime(18832): at com.jandans.geos.ImageDownloader.download(ImageDownloader.java:51)
06-06 09:38:19.686: E/AndroidRuntime(18832): at com.jandans.geos.vaikpied$1.onItemClick(vaikpied.java:98)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.widget.ListView.performItemClick(ListView.java:3513)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1849)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.os.Handler.handleCallback(Handler.java:587)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.os.Looper.loop(Looper.java:130)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.app.ActivityThread.main(ActivityThread.java:3835)
06-06 09:38:19.686: E/AndroidRuntime(18832): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 09:38:19.686: E/AndroidRuntime(18832): at java.lang.reflect.Method.invoke(Method.java:507)
06-06 09:38:19.686: E/AndroidRuntime(18832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
06-06 09:38:19.686: E/AndroidRuntime(18832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-06 09:38:19.686: E/AndroidRuntime(18832): at dalvik.system.NativeStart.main(Native Method)
`
Try below one –
Make sure the permission is added in your manifest file –
Simply customize this layout with your needs. Hope this helps you.