获取错误:无法解析 bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream) 的'compress'符号;



我收到编译器错误:

cannot resolve symbol for 'compress' for line: bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream); 

请让我知道问题出在哪里?

以下是课程:

public class EmployeeInfo extends ListActivity implements OnItemClickListener {
    EmpSQLiteHelper db = new EmpSQLiteHelper(this);
    List<Employee> list;
    ArrayAdapter<String> myAdapter;

    // get image from drawable
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.shyam);
    // convert from bitmap to byte array
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
    byte[] imageInByte = stream.toByteArray();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.emp_list);
        // drop this database if already exists
        db.onUpgrade(db.getWritableDatabase(), 1, 2);
        db.createEmpInfo(new Employee("Shyam", "28", imageInByte));
        db.createEmpInfo(new Employee("Ramesh", "30", imageInByte));
        db.createEmpInfo(new Employee("Anuj", "23", imageInByte));
        db.createEmpInfo(new Employee("Dinesh", "34", imageInByte));
        db.createEmpInfo(new Employee("Suresh", "29", imageInByte));
        db.createEmpInfo(new Employee("Arjun", "36", imageInByte));
        db.createEmpInfo(new Employee("Karn", "30", imageInByte));
        db.createEmpInfo(new Employee("Varun", "27", imageInByte));
        // get all employees
        list = db.getAllEmployeesInfo();
        List<String> listTitle = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            listTitle.add(i, list.get(i).getEmp_name());
        }
        myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listTitle);
        getListView().setOnItemClickListener(this);
        setListAdapter(myAdapter);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, EmpImageDisplay.class);
        intent.putExtra("employee", list.get(position).getEmp_Id());
        //startActivitiyForResult(intent, 1);
        startActivity(intent);
    }
    /*@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // get all books again, because //something changed
        list = db.getAllEmployeesInfo();
        List<String> listTitle = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            listTitle.add(i, list.get(i).getEmp_name());
        }
        myAdapter = new ArrayAdapter<String>(this,R.layout.emp_row_layout, R.id.emp_listName, listTitle);
        getListView().setOnItemClickListener(this);
        setListAdapter(myAdapter);
    }*/
}

如果需要更多详细信息,请告诉我。

您正在尝试在EmployeeInfo类的方法之外使用Bitmap类中的 compress 方法。 试着把这个:

// get image from drawable
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.shyam);
// convert from bitmap to byte array
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
byte[] imageInByte = stream.toByteArray();

在您的onCreate()方法中。

最新更新