Help needed to delete file from a plugin

179 views
Skip to first unread message

Pascal Georges

unread,
Jul 29, 2023, 2:16:24 AM7/29/23
to DroidScript
Hello,

I started to rewrite in a Java plugin some functions present in app (ListFolder, GetMetaData, IsFolder) in order to get better performance.
I am now stuck with file deletion on Android 11. I think I tried all possibilities from File.delete() to ContentResolver.query() and Context.deleteFile(), but it always fail with no error feedback that would help understand the issue.

Did anybody manage to delete files from a Java plugin ?

Pascal G. 

Cemal

unread,
Jul 29, 2023, 5:20:20 AM7/29/23
to DroidScript
Hello,
It may not be possible to solve it without seeing the error log.

If you are using the IDE, type $logcat in the Log tab and you can find your error in the logs.
If you want to check it on the device, go to the samples section at the top right (rocket icon). Press the Remote Terminal tab. Switch to the Device tab. Say connect. Then type logcat the latest log records will appear.

This will show the error logs and logs (that occurred in Java). After running the plugin, look at the logs.

Dave

unread,
Jul 29, 2023, 5:20:51 AM7/29/23
to DroidScript
Hi Pascal,

This should delete a file in the app's. sandbox (scoped storage area)

File f = new File( file );
if( f.exists() ) f.delete();

If you are outside the scoped storage area, things get more complicated as you need to get user permissions and use SAF.  

Pascal Georges

unread,
Jul 29, 2023, 5:27:22 AM7/29/23
to DroidScript
Hello,

The Java code does not raise errors, the function returns correctly but file.exists() is still true.
Note that I don"t have access to the remote terminal, being a non premium user.

Greetings

Pascal Georges

unread,
Jul 29, 2023, 5:50:40 AM7/29/23
to DroidScript
Hi Dave,

Below is the Java code I use, with no effective deletion wether the file being in private folder or Music one (/storage/emulated/0/Music/ or /sdcard/Music/).
I checked from the plugin I have the permissions READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE (but I don't have MANAGE_MEDIA).

As you see I try 3 times with different methods (this is just one of my many tries):
// ====================================================================
private String DeleteFile(Bundle b) throws Exception {
        String path = b.getString("p1");
        File file = new File(path);
        file.delete();
        if(file.exists()){
            file.getCanonicalFile().delete();
            if(file.exists()){
                m_ctx.deleteFile(file.getName());
            }
        }
        if (file.exists())
            return "Failed";
        else
            return "Success";
}
// ====================================================================
I have to do this because I noticed that file deletion takes about 100 ms + (files_in_dir * 10ms). So deleting a file in a folder with 1000, will take about 10 seconds. I don't have access to DS code, so why this delay when other apps delete files without any delay, whatever the number of files in the same directory ?

On a side note those calls could be optimized :
  • ListFolder: I get 50x speedup in java (see the code below)
  • IsFolder : I get only 2.5x speedup
  • GetMetaData (see code below) : same speed for one call but given that app.GetMetaData returns a string, with comma delimited result, it is not possible to split the result correctly when there is already commas in the artist, title, etc. So I return a JSON string that is easy to parse. That way I save 30 ms per extra tag fetched (because i only make one call, when with app.GetMetaData I have to make one different call for each tag).
// ====================================================================
    private String ListFolder(Bundle b) throws Exception {
        Log.d(TAG, "Got ListFolder");

        String dir = b.getString("p1");
        File directory = new File(dir);
        File[] files = directory.listFiles();
        ArrayList<String> paths = new ArrayList();

        for (File f : files) {
            paths.add(f.getPath());
        }

        JSONArray jsonArray = new JSONArray(paths);
        String s = jsonArray.toString();
        return s;
    }
// ====================================================================
    private String GetMetaData(Bundle b) throws JSONException {
        String path = b.getString("p1");

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(path);
        String artist = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
        String title = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
        String album = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
        String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        String bitrate = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE);

        JSONObject json = new JSONObject();
        json.put("artist", artist);
        json.put("title", title);
        json.put("album", album);
        json.put("artist", artist);
        json.put("duration", duration);
        json.put("bitrate", bitrate);

        return json.toString();
    }
// ====================================================================

Greetings

Pascal G.

Steve Garman

unread,
Jul 29, 2023, 6:12:51 AM7/29/23
to DroidScript
You don't need to be a premium user to use the wifi editor

In the Droidscript app press the Wifi editor button shown in the attached licture
An address will be displayed
Enter that address into a browser on the same network 

TopButtons.jpg

Pascal Georges

unread,
Jul 29, 2023, 8:10:03 AM7/29/23
to DroidScript
Hi Steve,

Yes, I know I can use the wifi editor but not the remote terminal as a free user.

Greetings

Pascal G.

Cemal

unread,
Aug 1, 2023, 4:18:20 AM8/1/23
to DroidScript
If you can use the Wi-Fi Editor you can go to the Debug tab and type $logcat.
Screenshot 2023-08-01 11.17.57.png

Pascal Georges

unread,
Aug 1, 2023, 4:45:39 AM8/1/23
to DroidScript
Thank you, I was not aware of that.

Greetings

Pascal G.

Reply all
Reply to author
Forward
0 new messages