2

I use AnkiDroid API to create and send cards to AnkiDroid application (https://github.com/ankidroid/Anki-Android/wiki/AnkiDroid-API). But there is one issue connected to sending media files via API. They are stored in the external storage, "/storage/emulated/0/AnkiDroid" in my case. I can add and read files from there, but I think this path may be different for other devices or in case if I switch to SD card. Question: how to correctly identify the path to application's external storage directory?

snailp4el
  • 153
  • 1
  • 15
  • I use code from example - "https://github.com/ankidroid/apisample" and it works well, except it does not have methods to send media. I can just send audio file to External storage and create link for this file. But i don not how to get path to the program's directory. Maybe it may be done throug the package name. `AddContentApi.getAnkiDroidPackageName(this);` – snailp4el Dec 04 '18 at 06:03

2 Answers2

1

Ex-AnkiDroid project maintainer here.

The path you have is the default path to the AnkiDroid media files.

AnkiDroid users can modify this path via configuration, as you probably know:

enter image description here

This setting is stored using Android's usual settings storage technology, which for security reasons is not accessible to other apps.

I don't think the property is currently exposed to other apps (source code), but that is a valid use case so please create a new issue at https://github.com/ankidroid/Anki-Android/issues

Meanwhile, and to be compatible with current and prior versions of AnkiDroid, I would say you have two options:

  • Scan the SD card looking for a called collection.media. This folder is inside the AnkiDroid folder, and its name can not be changed, by convention. If you find several, compare last modification dates and ask the user.
  • Have a setting similar to the one in the screenshot above. Users who want to use a non-default folder will have to set it up in both apps, which is a bit inconvenient but not such a crazy thing to ask for.
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • I thought about option number one. But phones may not have SD card at all. I think to give user option to choose the folder. But this solution does not look well. – snailp4el Dec 04 '18 at 10:14
  • I've actually switched to a phone that does NOT have SD card at all and I am not left with an app that will not work atm because it keeps referring to a wrong path "/storage/emulated/0/AnkiDroid" that apparently is invalid on my Google Pixel XL https://www.gsmarena.com/google_pixel_xl-8345.php – tbop Jan 01 '21 at 11:57
0

Attention hard code!!! I understand that it is not the best way. but it works for me.

public ArrayList getExternalFolderPath(String folderName){

    ArrayList<String> arrayList = new ArrayList<>();

    //get all storages in phone
    File externalStorages[] = getExternalFilesDirs(null);

    for (File es: externalStorages){
        String exF = es.toString();

        //cut out what don't need
        exF = exF.split("/Android/data")[0];

        File file = new File(exF);
        File[] files = file.listFiles();
        for (File f: files){
            Log.i(TAG, "getAnkiExternalFolder()" + f.toString());
            //add in array the path
            if(f.toString().contains(folderName)){
                arrayList.add(f.toString());
            }
        }
    }
    return arrayList;
}

also need add a manifest entry.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

And ask user access for reading if API level uper 23: https://developer.android.com/training/permissions/requesting

snailp4el
  • 153
  • 1
  • 15