在我的應用程序中,一個活動包含廚房,它可以在SD卡創建的文件夾中調用存儲所有圖像的順序(saved_images),每件事情都可以正常運行,但是如果刪除文件夾(saved_images),然後再次打開應用程序並保存任何圖像,應用程序將再次創建(saved_images)文件夾,但保存的圖像編號仍然記住SharedPreferences值並將圖像存儲在繼續no。以前保存刪除舊文件夾中的圖片,我的目標是獲得如下:再次保存,清除並保存SharedPreferences
一次從SD卡中刪除(saved_images)文件夾自動SharedPreferences必須cleard如果(saved_images)文件夾中創建它再次存儲從圖像開始意味着圖像-1,圖像-2等等。
我試着用這段代碼清除SharedPreferences,但它不起作用,可能會導致只存儲一張圖片,只能稱爲image-1。
2-如果我保存同一圖像兩次它保存,但我需要保存一次只有一次,當試圖再次保存它時必須給吐司:已保存。
這是一段代碼,我用它來清除和恢復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; }
,這原代碼,做工精細,所有的圖像存儲在順序
在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; }
我知道療法是有些東西丟失或錯誤的方式應用,所以請任何幫助方面將會理解,謝謝。
@ android116你檢查了我的答案... – GoCrazy 2013-04-22 10:34:19