I know this is a common question to ask but yet I have a problem which the ImageView
returns to null. I just want to know if it is possible to call an xml file from the drawable and convert it to Byte []
. One thing it returns a null because I didn't declare ImvTempCashCard = findViewById(R.id.ImageView);
.Is there any other way to call drawable image without using ImageView?.I just want to set temporary value to ImvTempCashCard
which assign image from the drawable files, It is necessary to use setBackgroundResource
to ImageView
?
error
Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference
Source
ImageView ImvTempCashCard;
ImvTempCashCard.setBackgroundResource(R.drawable.ic_creditcard);
Bitmap bitmap = ((BitmapDrawable)ImvTempCashCard.getDrawable()).getBitmap();
ImvTempCashCard.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 120, 120, false));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte [] byteimage = stream.toByteArray();
//byteimage = returns to null
Latest update:
Actually This will view and add to my GridView
, while looping the query, the condition is, when the blob image is null then it will replace the drawable files image. Did I implement it correctly?
try {
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT id,cash_card_actual_no,hh_number,series_number,cc_image, id_image, cash_card_scanned_no FROM CgList");
list.clear();
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
byte[] CashCardImage = cursor.getBlob(4);
if (CashCardImage.length ==1){
Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.ic_creditcard);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
byte [] z = stream.toByteArray();
CashCardImage = z;
}
list.add(new Inventory(cashCardNumber, CashCardImage,id));
}
adapter.notifyDataSetChanged();
}
Adapter inside onCreate
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory_list);
gridView = (GridView) findViewById(R.id.gridView);
list = new ArrayList<>();
adapter = new InventoryListAdapter(this, R.layout.activity_inventory_items, list);
gridView.setAdapter(adapter);
InventoryListAdapter class
public class InventoryListAdapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList<Inventory> foodsList;
public InventoryListAdapter(Context context, int layout, ArrayList<Inventory> foodsList) {
this.context = context;
this.layout = layout;
this.foodsList = foodsList;
}
@Override
public int getCount() {
return foodsList.size();
}
@Override
public Object getItem(int position) {
return foodsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
ImageView imageView;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.imageView = (ImageView) row.findViewById(R.id.imgFood);
row.setTag(holder);
}
else {
holder = (ViewHolder) row.getTag();
}
Inventory inventory = foodsList.get(position);
byte[] foodImage = inventory.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(foodImage, 0, foodImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}