Rohit Chauhan
Rohit Chauhan

Reputation: 1169

Android Delete File Using URI not working

So what i want is to delete file using URI

Do not flag is duplicate

I tried many answer but nothing worked here is the question i tried

How to delete file that is created using Uri?

Delete file using Uri

How to delete file that is created using Uri?

I write code to get images from gallery and then copy it to directory called ".blackhat" and delete original one (Kind of Move File Function)..But it's not working. even it is not generation Log so i can check the error. this code working proper for copying file but not deleting after copying....

if(requestCode == 2 && data.getData() !=null){
        if(cd == null){
            path.add(data.getData());
            Random rn = new Random();
            if(copyFileFromUri(this,data.getData(),String.valueOf(rn.nextInt(500)))){
                File fdelete = new File(data.getData().getPath());
                if (fdelete.exists()) {
                    if (fdelete.delete()) {
                        Log.d("delete","deleted");
                    } else {
                        Log.d("delete","not deleted");
                    }
                }
                Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
            }

        }else{
            for(int i=0;i<data.getClipData().getItemCount();i++){
                path.add(data.getClipData().getItemAt(i).getUri());
                Log.d("RjList",path.get(i).toString());
                Random rn=new Random();
                if(copyFileFromUri(this,data.getClipData().getItemAt(i).getUri(),String.valueOf(rn.nextInt(500)))){
                    File fdelete = new File(data.getClipData().getItemAt(i).getUri().getPath());
                    if (fdelete.exists()) {
                        if (fdelete.delete()) {
                            Log.d("delete","deleted");
                        } else {
                            Log.d("delete","not deleted");
                        }
                    }
                    Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

Where am i doing wrong ???? Thank in advance...

Upvotes: 0

Views: 2364

Answers (2)

Sujeet Kumar
Sujeet Kumar

Reputation: 276

File fdelete = new File(data.getData().getPath(),"here you should pass the file name");
            if (fdelete.exists()) {
                if (fdelete.delete()) {
                    Log.d("delete","deleted");
                } else {
                    Log.d("delete","not deleted");
                }
            }

Upvotes: 0

user10041244
user10041244

Reputation:

public static boolean delete(final Context context, final File file) {
final String pathone = MediaStore.MediaColumns.DATA + "=?";
final String[] selectedArgs = new String[] {
        file.getAbsolutePath()
};
final ContentResolver contentResolver = context.getContentResolver();
final Uri fileUri = MediaStore.Files.getContentUri("external");

contentResolver.delete(fileUri, pathone, selectedArgs );

if (file.exists()) {

    contentResolver.delete(fileUri, pathone, selectedArgs );
}
return !file.exists();
}

Upvotes: 1

Related Questions