Reputation: 47
I've a situation in which I need to populate an image view with an image from the user's external storage directory; I've been decoding a file using a Bitmap Factory and then setting the image view bitmap accordingly, but I keep running into Null Pointer Exceptions.
String path = Environment.getExternalStorageDirectory()+ "/Pictures/test.jpg";
File imgFile = new File(path);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imgView = (ImageView)findViewById(R.id.imageView2);
imgView.setImageBitmap(myBitmap);
} else {
Log.d("Ciaren", "File doesn't exist");
}
I'm running this directly in OnCreate, and the image view should be full screen, I can't for the life of me figure out what is throwing a null object though, as I've stepped through and all seems fine, the null pointer is thrown when the setImageBitmap(); method is called.
Upvotes: 2
Views: 1826
Reputation: 11422
The error is thrown in imgView.setImageBitmap(myBitmap); and thus imageView is null. As a result your imageView is not found. Make sure the resource id R.id.imageView2 is correct. Sometimes there are problems with eclipse and a Project > Clean can fix that too.
Upvotes: 1