如何动态添加图库中的图像

时间:2013-05-24 17:40:45

标签: android bitmap gallery android-gallery bitmapimage

我想在我的新图库中动态添加和降低图像。我将图像转换为字节数组(BitMap)并发送到另一个活动,它应该是我的新图库。当我使用一个图像时,这项工作很好。但当我转换为byteArray另一个图像并发送到我的新图库中的另一个活动时,第二个图像覆盖第一个图像。如何添加和删除两张图片和更多不同的图片。图片尺寸很小))))感谢很多功能响应。

有我发送照片的代码

private void saveFirstSelectedImageIdPreferences(String key, int position) {
    firstSelectionPositionId = position;
    sp = PreferenceManager.getDefaultSharedPreferences(this);
    Editor edit = sp.edit();
    edit.putInt(key, firstSelectionPositionId);
    Log.d(MY_LOG,"///// saving first selected image id/////////////////////"
                    + firstSelectionPositionId);
    edit.commit();
    //decode picture into byte array
    Bitmap btm = BitmapFactory.decodeResource(getResources(), R.drawable.sport_0);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    btm.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte [] byteArray = stream.toByteArray();
    //sending picture into favorite activity
    Intent intent = new Intent(this,Favorites.class);
    intent.putExtra("sport_0", byteArray);
    startActivity(intent);
    Log.d(MY_LOG,"convert first item to bytes");
    Toast.makeText(this, "Picture add to favorite", Toast.LENGTH_SHORT).show();


}

在这行代码中 byte [] byteArray = {(byte)R.drawable.sport_0} 我只能添加一个图像,就是这样。如何按下按钮点击添加按钮unlick删除图片录制图片。也许你知道解决这个问题的更好方法。请帮我解决这个问题。再次感谢你的回答。

最好的考虑Taras Matolinets

并且有代码活动,我想发送图片和我的新图库

final String MY_LOG = "mylog";
Gallery gallery;
ImageView imageView;
Bitmap btm;

byte[] byteArray = {(byte)R.drawable.sport_0};


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.favorite);

    imageView = (ImageView) findViewById(R.id.ImageFavoriteView);
    gallery = (Gallery) findViewById(R.id.FavoriteGallery);

    // creating AddImgadapter
    gallery.setAdapter(new AddImgAdapter(this));
    // getting gallery item click listener
    gallery.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> perent, View view,
                int position, long id) {
            imageView.setImageBitmap(btm);
            Log.d(MY_LOG, "img Id"+position);

        }
    });


    //finish activity
   // finish();
}


// new our adapter
public class AddImgAdapter extends BaseAdapter {
    int GalItemBg;
    Context count;

    public AddImgAdapter(Context c) {
        count = c;
        // taking Gallery attributes and resource id theme
        TypedArray typeArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typeArray.getResourceId(
                R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typeArray.recycle();


    }

    // amount of element
    @Override
    public int getCount() {

        return  byteArray.length;
    }


    // position of element
    @Override
    public Object getItem(int position) {

        return position;
    }
    // id of element
    @Override
    public long getItemId(int position) {

        return position;
    }
    //setting  view image resourses params  position and backgroud resourses

    @Override
    public View getView(int position, View converView, ViewGroup parent) {

        if(Sport.firstSelectionPositionId == Sport.selectedPosition){
        Bundle extrasFirst = getIntent().getExtras();
        byteArray = extrasFirst.getByteArray("sport_0");
        Log.d(MY_LOG, "covert bytes to first item");
        btm = BitmapFactory.decodeByteArray(byteArray, 0,
                byteArray.length);
        imageView.setImageBitmap(btm);
        }else if(Sport.secoundSelectedPositionId == Sport.selectedPosition){
            Bundle extrasSecond = getIntent().getExtras();
            byteArray = extrasSecond.getByteArray("sport_1");
            Log.d(MY_LOG, "covert bytes to second item");
            btm = BitmapFactory.decodeByteArray(byteArray, 0,
                    byteArray.length);
            imageView.setImageBitmap(btm);
        }


        ImageView newImageView = new ImageView(count);
        newImageView.setImageBitmap(btm);
        newImageView.setLayoutParams(new Gallery.LayoutParams(130, 100));
        newImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        newImageView.setBackgroundResource(GalItemBg);

        return newImageView;

    }

}

0 个答案:

没有答案
相关问题