如何调用解码函数来解码图像

时间:2013-02-16 15:16:52

标签: android

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

这一行:

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++;
    }

1 个答案:

答案 0 :(得分:1)

使用BitmapFatory.decodeResource(...)代替decodeFile(...)