在我的应用 InfiniteGallery 类中,通过按下按钮,您可以将任何图像保存到 SD 卡,以获取上次保存的编号并存储保存的图像的最后编号,以便您可以按顺序存储所有图像 ,但是当添加 SharedPreferences 时,我有红线下的错误:
1- SharedPreferences savedNumber = getSharedPreferences(PREFS_NAME, 0);
(在 :getSharedPreferences 下)。
错误是:创建方法'getSharedPreferences'(字符串,int)'
2- SharedPreferences saveNumber = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
(在:getApplicationContext下)。
错误是:创建方法'getApplicationContext()'
我试图修复它但是icant,任何建议或帮助将不胜感激,谢谢
我的代码:
View vi=convertView;
final ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.gallery_items, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.textView1);
holder.image=(ImageView)vi.findViewById(R.id.image);
holder.button=(Button)vi.findViewById(R.id.button_save);
bm = BitmapFactory.decodeResource( mContext.getResources(), images[itemPos]);
holder.image.setImageBitmap(bm);
holder.button.setOnClickListener(new OnClickListener() {
private Bitmap bm;
private String PREFS_NAME;
public void onClick(View arg0) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
if(!myDir.exists()){
myDir.mkdirs();}
SharedPreferences savedNumber = getSharedPreferences(PREFS_NAME, 0);
int lastSavedNumber = savedNumber.getInt("lastsavednumber",0);
lastSavedNumber++;
String fname = "Image-"+lastSavedNumber+".png";
File file = new File (myDir, fname);
if (file.exists ()) {file.delete (); }
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
//Your Bitmap from the resouce
out.flush();
out.close();
Toast.makeText(mContext, "Saved", Toast.LENGTH_LONG).show();}
catch (Exception e) {
e.printStackTrace(); }
SharedPreferences saveNumber = getApplicationContext().getSharedPreferences
(PREFS_NAME, 0);
SharedPreferences.Editor editorset = saveNumber.edit();
editorset.putInt("lastsavednumber",lastSavedNumber);
editorset.commit();
Toast.makeText(mContext, "button clicked", Toast.LENGTH_LONG).
show();}});
vi.setTag(holder);}
else holder=(ViewHolder)vi.getTag();
holder.text.setText(name[itemPos]);
final int stub_id=images[itemPos];
holder.image.setImageResource(stub_id);
return vi; }