-2

i am trying to get bitmap from drawable i use this code , but its not working because i can't use context inside a static

this is my full code fo this class :

package com.onazifi.shelf;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import org.antlr.runtime.BitSet;

import java.io.IOException;
import java.io.InputStream;

public class BookItem {

 private String author;
 private String title;
 private Bitmap image;

  Context context;

 public BookItem() {
  // TODO Auto-generated constructor stub
 }

 public BookItem(String _author, String _title , Bitmap _image) {
  this.author = _author;
  this.title = _title;
  this.image = _image;
 }

 public String getAuthor() {
  return this.author;
 }

 public void setAuthor(String _author) {
  this.author = _author;
 }

 public String getTitle() {
  return this.title;
 }

 public void setTitle(String _title) {
  this.title = _title;
 }

 public Bitmap getImage() {
  return this.image;
 }

 public void setImage(Bitmap _image) {
  this.image = _image;
 }


 Bitmap _image = BitmapFactory.decodeResource(context.getResources(),
   R.drawable.gbrankhalil2);



 public static final BookItem[] ALL_BOOKS={

        new BookItem("1","test" ,BitmapFactory.decodeResource(context.getResources(),
    R.drawable.gbrankhalil2) )
       

    };
}

the problem is here

BitmapFactory.decodeResource(context.getResources(),
    R.drawable.gbrankhalil2)

i can't call context inside static , how can i call drawable from inside static using bitmap

UPDATE :

this is where the array list exist

public class Library {

 private ArrayList<BookItem> arrayBookItem;
 public static final int AUTHOR = 1;
 public static final int TITLE = 2;
 public static final int IMAGE = 3;

 public static final int RATE = 4;
 public static final int DOWNLOAD_DATE =5;

 public Library() {
  arrayBookItem = new ArrayList<BookItem>();
 }

 public void setColectionBookItem(ArrayList<BookItem> _array) {
  this.arrayBookItem = _array;
 }

 public void addBookItem(BookItem _bi) {
  this.arrayBookItem.add(_bi);
 }

 public ArrayList<ArrayList<BookItem>> groupbyArrayBookItem(int type) {

  BookItem[] books = BookItem.ALL_BOOKS;
  ArrayList<ArrayList<BookItem>> groupList = new ArrayList<ArrayList<BookItem>>();
  String getType = "";
  
  switch (type) {
  case AUTHOR:
   getType = "bookitem.getAuthor()";
   break;
  case TITLE:
   getType = "bookitem.getTitle()";
   break;
   case IMAGE:
    getType = "bookitem.getImage";
    break;
  case DOWNLOAD_DATE:
   getType = "bookitem.getDownloadDate()";
   break;
  case RATE:
   getType = "bookitem.getRate()";
   break;
  default:
   return groupList;
  }

  /*
   * books is a object of BookItem
   * bookitem is item for point to list
   * getType is a string value for set type of grouping
   * groupbyArrayBookItem return back array of array of items
   */
  Iterable<Group> groups = 
    from("bookitem").in(books).group("bookitem")
    .by(getType).into("g").select("g");

  for (Group group : groups) {
   ArrayList<BookItem> obj = new ArrayList<BookItem>();
   for (Object Item : group.getGroup()) {
    obj.add((BookItem) Item);
   }
   groupList.add(obj);
  }

  return groupList;
 }
}

1 Answers1

0

Pass context through your constructor then use it.

public BookItem(Context context, String _author, String _title , Bitmap _image) {
    this.context = context;
    this.author = _author;
    this.title = _title;
    this.image = _image;
}

OR

public BookItem(Context context) {
    this.context = context;
}

Then use it.

BitmapFactory.decodeResource(context.getResources(),
            R.drawable.gbrankhalil2)

To call it in any activity.

BookItem item = new BookItem(MainActivity.this, _author, _title, _image);

OR if it is fragment

BookItem item = new BookItem(getContext(), _author, _title, _image);
Omkar
  • 1,493
  • 3
  • 17
  • 36
  • i have tried exactly as u said but i am still getting error non static field context cannot be referenced from a static field – Alhaddad Soft Mar 06 '16 at 14:40
  • populate the item array in your activity or fragment. For more regarding your error and understanding check this post http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context – Omkar Mar 06 '16 at 15:25