-1

如何使用解码功能来压缩和解码我的图像?如何调用我的解码功能?

这一行:

image = decodeFile(getResources(),R.drawable.my);

显示错误:

SQLiteDemoActivity 类型中的方法 decodeFile(File) 不适用于参数 (Resources, int)

编码:

  ArrayList<Contact> imageArry = new ArrayList<Contact>();
 ContactImageAdapter adapter;
   Button BrowseButton;
  Button BrowseButton2;
 DataBaseHandler db;
  public  static Bitmap image ;
 public  static byte[] imageInByte;



 ByteArrayOutputStream stream = new ByteArrayOutputStream();
/** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  db = new DataBaseHandler(this);




  BrowseButton=(Button)findViewById(R.id.BrowseButton);
  BrowseButton2=(Button)findViewById(R.id.BrowseButton2);

 adapter = new ContactImageAdapter(this, R.layout.screen_list,  imageArry);

  BrowseButton.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View v) {



   < this line show error--->     image = decodeFile(getResources(),R.drawable.my);
    //BitmapFactory.decodeFile(photo.toString(),options);
    image.compress(CompressFormat.JPEG, 100, stream);
     imageInByte = stream.toByteArray();

    Log.d("Insert: ", "Inserting ..");
    db.addContact(new Contact("FaceBook", imageInByte));



}
   });

    BrowseButton2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {


    List<Contact> contacts = db.getAllContacts();
    for (Contact cn : contacts) {




    String log = "ID:" + cn.getID() + " Name: " + cn.getName()      + " ,Image: " + cn.getImage();


    //Writing Contacts to log
    Log.d("Result: ", log);
    //add contacts data in arrayList
    imageArry.add(cn);

    }

    ListView dataList = (ListView) findViewById(R.id.list);
    dataList.setAdapter(adapter);

    try {
        stream.close();
        stream = null;
    } catch (IOException e) {

        e.printStackTrace();
    }

 }
   });



             private Bitmap decodeFile(File f) {
 try {
    // decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(new FileInputStream(f), null, o);

    // Find the correct scale value. It should be the power of 2.
    final int REQUIRED_SIZE = 70;
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale++;
    }
4

1 回答 1

1

使用BitmapFatory.decodeResource(...)而不是decodeFile(...).

于 2013-02-16T15:51:40.830 回答