Android 如何从SharedReferences文件中动态添加和删除库中的项目

Android 如何从SharedReferences文件中动态添加和删除库中的项目,android,gallery,sharedpreferences,image-gallery,Android,Gallery,Sharedpreferences,Image Gallery,我可以在应用程序中单击按钮保存和删除sharePreferences文件中的项目。当我按下按钮时,选定的项目将显示在下一个Favorite_活动的Gallery中。如何从SharedReferences文件中动态添加和删除Gallery-Favorite_活动中的项目?感谢帮助 这是我的图库代码 int[] imgID = { R.drawable.health, R.drawable.health_1, R.drawable.health_2, R.drawable.healt

我可以在应用程序中单击按钮保存和删除sharePreferences文件中的项目。当我按下按钮时,选定的项目将显示在下一个Favorite_活动的Gallery中。如何从SharedReferences文件中动态添加和删除Gallery-Favorite_活动中的项目?感谢帮助

这是我的图库代码

int[] imgID = { R.drawable.health, R.drawable.health_1, R.drawable.health_2,
        R.drawable.health_3, R.drawable.health_4, R.drawable.health_5,
        R.drawable.health_6, R.drawable.health_7, R.drawable.health_8,
        R.drawable.health_9,R.drawable.health_10,R.drawable.health_11 };

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.favorite);
    imageView = (ImageView) findViewById(R.id.ImageFavoriteView);
    imageView.setImageResource(imgID[0]);

    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.setImageResource(imgID[position]);
            Log.d(MY_LOG, "img Id"+position);

        }
    });

}
// 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 imgID.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) {

        ImageView newImageView = new ImageView(count);
        newImageView.setImageResource(imgID[position]);
        newImageView.setLayoutParams(new Gallery.LayoutParams(130, 100));
        newImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        newImageView.setBackgroundResource(GalItemBg);

        return newImageView;

    }

}
int[]imgID={R.drawable.health,R.drawable.health_1,R.drawable.health_2,
R.drawable.health_3,R.drawable.health_4,R.drawable.health_5,
R.drawable.health_6,R.drawable.health_7,R.drawable.health_8,
R.drawable.health_9,R.drawable.health_10,R.drawable.health_11};
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite);
imageView=(imageView)findViewById(R.id.ImageFavoriteView);
setImageResource(imgID[0]);
画廊=(画廊)findViewById(R.id.FavoriteGallery);
//创建AddImgadapter
gallery.setAdapter(新的AddImgAdapter(本));
//获取库项目单击侦听器
gallery.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(适配器视图、视图、,
内部位置,长id){
setImageResource(imgID[position]);
日志d(我的日志,“img Id”+位置);
}
});
}
//我们的新适配器
公共类AddImgAdapter扩展了BaseAdapter{
int GalItemBg;
上下文计数;
公共AddImgAdapter(上下文c){
计数=c;
//获取库属性和资源id主题
TypedArray typeArray=获取StyledAttributes(R.styleable.GalleryTheme);
GalItemBg=typeArray.getResourceId(
R.styleable.GalleryTheme_android_galleryItemBackground,0);
typeArray.recycle();
}
//元素量
@凌驾
public int getCount(){
返回imgID.length;
}
//元素位置
@凌驾
公共对象getItem(int位置){
返回位置;
}
//元素的id
@凌驾
公共长getItemId(int位置){
返回位置;
}
//设置查看图像资源参数位置和背景资源
@凌驾
公共视图getView(内部位置、视图对流视图、视图组父视图){
ImageView newImageView=新ImageView(计数);
setImageResource(imgID[position]);
newImageView.setLayoutParams(newGallery.LayoutParams(130100));
newImageView.setScaleType(ImageView.ScaleType.CENTER_-INSIDE);
newImageView.setBackgroundResource(GalItemBg);
返回newImageView;
}
}

添加图像-如果您的图像太大,位图将给出错误,因此您必须在下面编写代码以调整图像大小。在下面的函数中传递文件

Bitmap bitmap = decodeFile(f1);
imgView.setImageBitmap(bitmap);

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);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 150;

    // Find the correct scale value. It should be the power of 2.
    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 *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

} catch (FileNotFoundException e) {
}
return null;
下面是删除图像后刷新库的代码

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" +  Environment.getExternalStorageDirectory())));
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" +  Environment.getExternalStorageDirectory())));