BossWalrus
BossWalrus

Reputation: 770

Delete Image from Gridview

I am trying to simply delete an image from a gridview. I was having troubles earlier with nothing deleting, but now that isn't a problem since I determined that I just need the correct file path. The Images are saved on my sd card, so I need help with the code that will correctly identify the right file path of the image that has been clicked, and to delete it when I click the delete button. What am I doing wrong? No errors are thrown, and my manifest is up to date on all permissions. I thought it would be a lot simpler than it has turned out to be. The following is my code:

    deletebutton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    int id = viewIt.getId();
    Log.i("start", "BEGIN_DELETE");
    String myPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/My Directory/";
    File file= new File(myPath + intent.getData());

if(file.exists()){  
    file.delete();
}
    Log.i("end", "END_DELETE");

    dialog.dismiss();                                                                       

    }
    });

Upvotes: 1

Views: 2065

Answers (1)

BossWalrus
BossWalrus

Reputation: 770

Figured it out! I used a textview to view what file path was getting returned, and then played around with it until I got the correct file path. Then it worked like a charm. Below is my working code.

delete.setOnClickListener(new View.OnClickListener() {


                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    int id = viewIt.getId();
                                     Log.i("start", "BEGIN_DELETE");
                                        String filestring = arrPath[id];
                                        filepath.setText(filestring);
                                        File myDir = new File(filestring);
                                        if(myDir.exists()) 
                                        {
                                            myDir.delete();
                                        }
                                        dia.dismiss();
                                        Log.i("end", "END_DELETE"); 

                                }
                            });

Upvotes: 1

Related Questions