2

I'm working on android app in which i parse the data from the webservice and then store it in the database. here i also want to store the image in the database and retrive back in the next activity.

I'm using this code for inserting the image in database.

Bitmap yourSelectedImage;
ByteArrayOutputStream stream;
byte[] byteArray;

this is in the for loop

{ 
yourSelectedImage = BitmapFactory.decodeFile(SingleImageURL[i]);
stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100,
                    stream);
byteArray = stream.toByteArray();

database.insertDetail(ID[i],byteArray[i]); 

}

Here SingleImageURL have the image url that is coming from the webservice.

when i'm run the code it will give error nullPointerException in this line

 yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100,
                    stream);

please tell me where i'm going wrong. and how to do this.

Kai
  • 38,985
  • 14
  • 88
  • 103
Preet_Android
  • 2,332
  • 5
  • 25
  • 43

1 Answers1

3

Load your Bitmap yourSelectedImage using this link. and then save it to database by converting it to byte[]

You are decoding a file, which actually is a URL. So ur yourSelectedImage is null. And when you try to compress it using

yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);

you get NullPointerException

Community
  • 1
  • 1
AnujMathur_07
  • 2,586
  • 2
  • 18
  • 25