我可以在我的应用程序中单击按钮时保存和删除sharePreferences文件中的项目。当我按下按钮时,所选项目应显示在下一个Favorite_Activity的Gallery中。如何动态添加和删除来自SharedPreferences的gallery- 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;
}
}
答案 0 :(得分: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())));