0

我可以在我的应用程序中单击按钮时保存和删除 sharePreferences 文件中的项目。当我按下按钮时,所选项目应显示在下一个 Favorite_Activity 的图库中。如何从 SharedPreferences 文件动态添加和删除图库中的项目-Favorite_Activity?谢谢求助。

有我的画廊代码

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;

    }

}
4

1 回答 1

0

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

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;

}

删除:-使用下面的代码可能会对您有所帮助。

        File fdelete = new File(file_dj_path);
    if (fdelete.exists()) {
        if (fdelete.delete()) {
            System.out.println("file Deleted :" + file_dj_path);
        } else {
            System.out.println("file not Deleted :" + file_dj_path);
        }
    }

下面是删除图像后刷新图库的代码

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" +  Environment.getExternalStorageDirectory())));
于 2013-05-23T09:31:44.083 回答