我尝试保存具有图像类型的油漆。然后将图像转换为位图以保存SQLite数据库。我在“位图位图= BitmapFactory.decodeByteArray(paint,100,paint.length);”行上收到错误“ java.lang.ArrayIndexOutOfBoundsException”。我该如何解决?
CanvasActivity.java
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
relativeLayout.setDrawingCacheEnabled(true);
Bitmap bitmap = relativeLayout.getDrawingCache();
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArray);
byte[] paint = byteArray.toByteArray();
MainActivity.database.Insert_Item(editTextName.getText().toString().trim(), paint);
Toast.makeText(CanvasActivity.this, "Added a paint.", Toast.LENGTH_SHORT).show();
startActivity(new Intent(CanvasActivity.this, MainActivity.class));
}
});
PaintAdapter.java
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
// Initialize view and mapping
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(layout, null);
viewHolder.textViewName = (TextView) convertView.findViewById(R.id.textViewName);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Set items
PaintItem paintItem = itemList.get(position);
viewHolder.textViewName.setText(paintItem.getName());
// Convert byte[] -> bitmap
byte[] paint = paintItem.getPaint();
Bitmap bitmap = BitmapFactory.decodeByteArray(paint, 100, paint.length);
viewHolder.imageView.setImageBitmap(bitmap);
return convertView;
}
答案 0 :(得分:1)
我认为可能是由1.油漆引起的。 2. paint小于100个字节,因此当偏移量要从100位置开始时会出现此错误。 您确定您的绘画文件已完整提供给decodeByteArray吗?
答案 1 :(得分:1)
我建议您通过以下方式将图像保存到数据库:图像到base64代码, base64 utils在android.util
下