保存,清除和保存共享preferences再次 [英] Save , Clear and Save SharedPreferences again

查看:206
本文介绍了保存,清除和保存共享preferences再次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,一个活动包含删除此文件夹的情况下,厨房这使存储的所有顺序影像的SD卡创建的文件夹名为(saved_images),运行正常的每一件事情, BUT ( saved_images)因任何原因,然后重新打开应用程序并保存任何图像,应用程序将再次创建(saved_images)文件夹,但保存的图像数量还记得不继续共享preferences价值和存储图像。 $的对$ pvious保存删除旧文件夹中的图片,我的目标是获得以下内容:

In my app , one activity contain galley which enable store all the images in sequential order in SD card created folder called ( saved_images ) , every thing run fine , BUT in case of delete this folder ( saved_images ) for any reason , then open app again and save any image , app will create the ( saved_images ) folder again but saved image number still remember the SharedPreferences value and store images in continued no. of previous saved images of deleted old folder , my goal to get the following:

1 - 一次(saved_images)从SD卡自动共享preferences删除的文件夹必须cleard并重新创建如果(saved_images)文件夹将其存储从中意味着图像1日开始的图像,图像-2,依此类推。

1- once ( saved_images ) folder deleted from SD card Automatically SharedPreferences must be cleard and if ( saved_images ) folder created again it store images from the beginning which mean image-1,image-2,and so on .

我尝试这个code清除共享preferences但它不工作的或许是导致存储一幅图像只能称为图像-1只。

i tried with this code to clear the SharedPreferences but it doesnt work probably it lead to store one image only called image-1 only .

2 - 如果我保存相同的图像两次,一次是保住了,但我只需要一次保存一段时间,当尝试保存再次就一定要把面包:已经保存

2- if i save the same image twice a time it saved , but i need to save it once a time only and when try to save it again must give Toast : already saved .

这是我用来清除并恢复共享preferences片code的:

this is the piece of code which i used to clear and restore the SharedPreferences :

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

        holder.button.setOnClickListener(new OnClickListener() {

  private Bitmap bm;
public void onClick(View arg0) {
    String root = Environment.getExternalStorageDirectory().toString();
       File myDir = new File(root + "/saved_images");
       if(!myDir.exists()){
           myDir.mkdirs();
    bm = BitmapFactory.decodeResource( mContext.getResources(), images[itemPos]);
         holder.image.setImageBitmap(bm);
 SharedPreferences savedNumber = mContext.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);
        out.flush(); 
        out.close(); }
         catch (Exception e) { 
        e.printStackTrace(); }

        SharedPreferences saveNumber = mContext.getApplicationContext
                  ().getSharedPreferences(PREFS_NAME, 0); 
        SharedPreferences.Editor editorset = saveNumber.edit(); 
        editorset.putInt("lastsavednumber",lastSavedNumber); 
        editorset.commit();}

  else if(!myDir.delete()){
    SharedPreferences saveNumber1 = mContext.getApplicationContext
         ().getSharedPreferences(PREFS_NAME, 0); 
    SharedPreferences.Editor editor = saveNumber1.edit();
    editor.clear(); 
    editor.commit();

Toast.makeText(mContext, "saved", Toast.LENGTH_SHORT). 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; } 

和原来这个code这工作得很好,所有的图像存储顺序

and this the original code which work fine and store all the images in sequential order

在SD卡上的文件夹。

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

        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();}
   bm = BitmapFactory.decodeResource( mContext.getResources(), images[itemPos]);
         holder.image.setImageBitmap(bm);
 SharedPreferences savedNumber = mContext.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); 
        out.flush(); 
        out.close(); }
         catch (Exception e) { 
        e.printStackTrace(); 
        } 

    SharedPreferences saveNumber = mContext.getApplicationContext 
                 ().getSharedPreferences(PREFS_NAME, 0); 
    SharedPreferences.Editor editorset = saveNumber.edit(); 
    editorset.putInt("lastsavednumber",lastSavedNumber); 
    editorset.commit();
  Toast.makeText(mContext, "saved", Toast.LENGTH_SHORT). 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; } 

我知道疗法是有些东西丢失或错误的方式应用,所以请任何帮助方面,将AP preciated,谢谢。

i know ther is some thing missing or applied in wrong way , so please any help regard it will be appreciated , thanks.

推荐答案

问题1

虽然删除您的文件夹,你必须设置你的共享preferences值为零。因此,这将与当前的code工作,像你所期望

While deleting your folder you have to set the your SharedPreferences Value to be zero. So it will work with your current code and as like you expected

if(!myDir.exists()){ 
myDir.mkdirs();
SharedPreferences saveNumber = mContext.getApplicationContext().getSharedPreferences(PREFS_NAME, 0); 
SharedPreferences.Editor editorset = saveNumber.edit(); 
editorset.putInt("lastsavednumber",0); 
editorset.commit();
}

有关问题2
创建一个数据库来对图像保存,包括/未保存状态的所有细节。

For Question 2 Create a database to all the details about the image including saved/unsaved state.

相反在阵列中存储的值,你必须存储在数据库。

Instead of storing values in array you have to store that in to database.

在到SD卡保存图像之前,你必须检查与数据库存储/保存的状态。如果没有保存,那么你可以保存图像,你也有作为保存只有映像来更新数据库列状态。当用户presses你能找到保存图像相同的图像,以便下一次。在这里你可以吐司如已经保存的图像。

Before saving the image in to sdcard, you have to check with the database saved/unsaved state. If not saved then you can save the image and also you have to update the database column state as saved for that image only. So next time when the user presses the same image you can find that the image is saved. Where you can Toast as Image already saved.

这篇关于保存,清除和保存共享preferences再次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆