我已经看过有关如何从SD卡中删除图像的所有其他帖子。 我知道之前已经问过,我已经实施了人们提出的所有建议,但是我一直没有得到任何结果。 所有图像都保留在我的SD卡目录中。 所以我希望有人能找到我做错了什么,因为我在这里完全迷失了......
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cookbook);
final GridView gridview = (GridView) findViewById(R.id.gridView);
myImageAdapter = new ImageAdapter(this);
gridview.setAdapter(myImageAdapter);
gridview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(CookbookActivity.this, "You've removed an item", Toast.LENGTH_SHORT).show();
// Removes the image from the gridview
myImageAdapter.remove(position);
// Updates the gridview
myImageAdapter.notifyDataSetChanged();
int i = 0;
i++;
// Trying to delete the LongClicked Image from the SD card
String myFile = "/Food Inspiration/" + "pic-" + i + ".png";
String myPath = Environment.getExternalStorageDirectory()+myFile;
File f = new File(myPath);
Boolean deleted = f.delete();
return true;
}
});
更新 我发现这是因为我的图像的文件路径。 我有一个while循环来使图像名称增加。 所以Image1,Image2等等。
代码
int i = 0;
do {
sdImageMainDirectory = new File(root, "pic-" + i + ".png");
i++;
} while (sdImageMainDirectory.exists());
更新2.0 好吧,所以我设法用这个循环一次删除所有项目:
// Deletes EVERY image when LongClicked... cant make it delete 1 image
for (int i = 0; i < 100; i++){
// Trying to delete the LongClicked Image from the SD card
String myFile = "/Food Inspiration/" + "pic-" + i + ".png";
String myPath = Environment.getExternalStorageDirectory()+myFile;
File f = new File(myPath);
Boolean deleted = f.delete();
}
有谁知道如何只保存一个我长按的项目,而不是一次保存每个项目。我知道我删除了所有内容,因为我&lt; 100但这仅用于测试目的。我希望有人能够回答如何一次删除1个项目。