简体   繁体   中英

Delete file using uri extracted from MediaStore.Audio.Media.DATA

Before marking this question as duplicate i wanna say that i had visited many similar questions but none of them had solved my problem.So, i had extracted all audio files from internal storage using MediaStore and stored its Uri in string form but later when i try to use file.delete using that Uri to delete the audio, former is returning false. Following goes the code for extracting song uri and its working properly:

    Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection=MediaStore.Audio.Media.IS_MUSIC+"!=0";
    Cursor c=getContentResolver().query(uri,null,selection,null,null);
    if(c!=null)
    {
        c.moveToFirst();

        do {
            String audioName=c.getString(c.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
            String artist=c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            String album=c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM));
            String url=c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
            String size=c.getString(c.getColumnIndex(MediaStore.Audio.Media.SIZE));

            audioInfoArrayList.add(new AudioInfo(url,audioName,artist,album));

        }while (c.moveToNext());

        c.close();

Here, i had stored all the retrieved information about audio in arrayList of a custom type which also stores above extracted uri in string form which could be extracted using getUrl() method. Following goes the code how i'am trying to delete it:

Uri uri= Uri.parse(audioInfo.get(position).getUrl()); //its actually returning the url stored above in string form
deleteFile(uri.getPath());
Log.i("logText","uri.getPath() : "+uri.getPath());
Log.i("logText","audioInfo.get(position).getUrl() : "+audioInfo.get(position).getUrl());

deleteFile function:

public void deleteFile(String filePath){
    if(filePath.startsWith("content://")){
        ContentResolver contentResolver = context.getContentResolver();
        contentResolver.delete(Uri.parse(filePath), null, null);
    }else {
        File file = new File(filePath);
        if(file.exists()) {
            if (file.delete()) {
                Log.e("logText", "File deleted."); //condition 0
            }else {
                Log.e("logText", "Failed to delete file!");  //condition 1
            }
        }else {
            Log.e("logText", "File not exist!"); //condition 2
        }
    }
} 

And finally my logcat for a sample audio file:

 01-11 13:52:34.257 19982-19982/com.example.hp.myplayer E/logText: Failed to delete file! 01-11 13:52:34.257 19982-19982/com.example.hp.myplayer I/logText: uri.getPath() : /storage/emulated/0/bluetooth/Copy Copy Fireflies-Owl_City[www.Mp3MaD.Com].mp3 01-11 13:52:34.257 19982-19982/com.example.hp.myplayer I/logText: audioInfo.get(position).getUrl() : /storage/emulated/0/bluetooth/Copy Copy Fireflies-Owl_City[www.Mp3MaD.Com].mp3 

Note: On calling deleteFile() condition 1 is executed not 2 or 0 which according to me means that file is being located using that uri in the storage but can't be deleted and file.delete() is returning false.

Requesting for read and write external storage permission in manifest is not enough for Android 6 and above.

You should add code to ask the user of your app to confirm the requested permissions.

Google for runtime permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM