android如何添加浏览按钮?

时间:2012-12-13 08:27:56

标签: android

我跟着this tutorial

这有助于我在数据库中插入图像。但是这段代码一次又一次地将相同的图像硬编码到数据库中。

如何修改此代码以允许我通过单击我自己选择的浏览按钮和文本视图从内存中上传我选择的照片?

  import java.io.ByteArrayOutputStream;
  import java.util.ArrayList;
  import java.util.List;

   import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
     import android.os.Bundle;
    import android.util.Log;
     import android.widget.ListView;

    public class SQLiteDemoActivity extends Activity {
     ArrayList<Contact> imageArry = new ArrayList<Contact>();
  ContactImageAdapter adapter;

    /** Called when the activity is first created. */
    @Override
     public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     DataBaseHandler db = new DataBaseHandler(this);
    //get image from drawable
    Bitmap image = BitmapFactory.decodeResource(getResources(),
   R.drawable.facebook);

    //convert bitmap to byte
     ByteArrayOutputStream stream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
   byte imageInByte[] = stream.toByteArray();
    /**
    * CRUD Operations
   * */
     //Inserting Contacts
  Log.d("Insert: ", "Inserting ..");
   db.addContact(new Contact("FaceBook", imageInByte));
  //display main List view bcard and contact name

  //Reading all contacts from database
     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);

    }
      adapter = new ContactImageAdapter(this, R.layout.screen_list,
    imageArry);
    ListView dataList = (ListView) findViewById(R.id.list);
   dataList.setAdapter(adapter);
  }
     }










       public class Contact {

// private variables
int _id;
String _name;
byte[] _image;

// Empty constructor
public Contact() {

}

// constructor
public Contact(int keyId, String name, byte[] image) {
this._id = keyId;
this._name = name;
this._image = image;

}

// constructor
public Contact(String contactID, String name, byte[] image) {
this._name = name;
this._image = image;

}

// constructor
public Contact(String name, byte[] image) {
this._name = name;
this._image = image;
}

// getting ID
public int getID() {
return this._id;
}

// setting id
public void setID(int keyId) {
this._id = keyId;
}

// getting name
public String getName() {
return this._name;
}

// setting name
public void setName(String name) {
this._name = name;
}

// getting phone number
public byte[] getImage() {
return this._image;
}

// setting phone number
public void setImage(byte[] image) {
this._image = image;
}









 import java.io.ByteArrayInputStream;
  import java.util.ArrayList;

  import android.app.Activity;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

  public class ContactImageAdapter extends ArrayAdapter<Contact>{
  Context context;
  int layoutResourceId;
  //BcardImage data[] = null;
 ArrayList<Contact> data=new ArrayList<Contact>();
 public ContactImageAdapter(Context context, int layoutResourceId, 
ArrayList<Contact>    data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
 this.data = data;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 View row = convertView;
 ImageHolder holder = null;
 if(row == null)
 {
 LayoutInflater inflater = ((Activity)context).getLayoutInflater();
 row = inflater.inflate(layoutResourceId, parent, false);
 holder = new ImageHolder();
 holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
   holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
  row.setTag(holder);
 }
  else
 {
  holder = (ImageHolder)row.getTag();
  }
 Contact picture = data.get(position);
 holder.txtTitle.setText("facebook");
 //convert byte to bitmap take from contact class
 byte[] outImage=picture._image;
 ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
 Bitmap theImage = BitmapFactory.decodeStream(imageStream);
  holder.imgIcon.setImageBitmap(theImage);
 return row;
  }
static class ImageHolder
  {
 ImageView imgIcon;
 TextView txtTitle;
 }
 } 






      import java.io.ByteArrayInputStream;
  import java.util.ArrayList;

 import android.app.Activity;
 import android.content.Context;
 import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

  public class ContactImageAdapter extends ArrayAdapter<Contact>{
 Context context;
  int layoutResourceId;
//BcardImage data[] = null;
 ArrayList<Contact> data=new ArrayList<Contact>();
 public ContactImageAdapter(Context context, int layoutResourceId, 
 ArrayList<Contact>    data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
  this.data = data;
  }

 @Override
  public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
    ImageHolder holder = null;
    if(row == null)
    {
  LayoutInflater inflater = ((Activity)context).getLayoutInflater();
     row = inflater.inflate(layoutResourceId, parent, false);
   holder = new ImageHolder();
    holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
    holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
      row.setTag(holder);
  }
   else
   {
    holder = (ImageHolder)row.getTag();
 }
   Contact picture = data.get(position);
    holder.txtTitle.setText("facebook");
  //convert byte to bitmap take from contact class
   byte[] outImage=picture._image;
   B yteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
   Bitmap theImage = BitmapFactory.decodeStream(imageStream);
   holder.imgIcon.setImageBitmap(theImage);
   return row;
  }
  static class ImageHolder
    {
     ImageView imgIcon;
   TextView txtTitle;
  }
      }

1 个答案:

答案 0 :(得分:0)

您应该阅读此主题以使用gallery选择图像,然后将其插入数据库: How to pick an image from gallery (SD Card) for my app?