I have tried following code in which i am trying to save my uploaded image into SD card folder with some name.It gives null pointer exception on the mentioned line. Any suggestions please.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
Code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==i && resultCode==RESULT_OK && data != null){
Uri selectedImage = data.getData();
coverpic.setImageURI(selectedImage);
Bitmap image =( (BitmapDrawable)coverpic.getDrawable()).getBitmap();//This line throws the exception
if (!direct.exists()) {
File wallpaperDirectory = new File("/images/");
wallpaperDirectory.mkdirs();
}
Bundle extras = getIntent().getExtras();
File file = new File(new File("/images/"),extras.getString("name")+i + ".jpg" );
if (file.exists()) {
file.delete();
}
try{
FileOutputStream out=new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.JPEG,100,out);
out.flush();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}