В настоящее время я читаю изображение из галереи в формате растрового изображения. При сохранении его в базе данных мне нужно преобразовать его в байты, в то время как в классе адаптера изображения мне нужно преобразовать его в растровое изображение.Преобразование битмапа в байт и наоборот
Ниже приведен код: - преобразование в байт для того, чтобы сохранить его в базе данных
public void submitAction(View view)
{
/*This method creates a new post and populates it with the data added by the user. The data is then stored in the database
* using the Active Android Library.*/
Post p = new Post();
EditText title = (EditText) findViewById(R.id.post_title_input);
String tit = title.getText().toString();
EditText description = (EditText)findViewById((R.id.editText));
String desc = description.getText().toString();
Bitmap img = yourSelectedImage;
p.title=tit;
p.description=desc;
p.section="science";
int bytes = img.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
img.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
}
код в классе imageAdapter - преобразовать байт [] в растровое изображение
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
/*Converting image to byte*/
Post p = posts.get(position);
byte[] image = p.image;
ByteArrayInputStream imageStream = new ByteArrayInputStream(image);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(theImage);
return imageView;
}
Когда стереосистеме и обновите приложение, оно сбой в строке Bitmap theImage = BitmapFactory.decodeStream(imageStream);
и завершается исключением нулевого указателя.