简体   繁体   中英

How to get downloaded models in ML Kit Translation

I'm trying to check if a model has been downloaded for translation in ML Kit Translation. I did not find any official documentation for this and wrote the code looking at the sample application from Google.

This code checks if among the downloaded models there is the right one:

public static boolean isLanguageDownloaded(String language) {
    boolean isDownloaded = false;
    for (int i = 0; i < getAvailableModels().size(); i++) {
        if (getAvailableModels().get(i).equals(language)) {
            isDownloaded = true;
        } else {
            isDownloaded = false;
        }
    }
    return isDownloaded;
}

This code for getting available model:

public static List<String> getAvailableModels() {
    List<String> availableModels = new ArrayList<>();
    getRemoteModelManager()
            .getDownloadedModels(TranslateRemoteModel.class)
            .addOnSuccessListener(
                    models -> {
                        // Model downloading is complete.
                        // ...
                        for (TranslateRemoteModel model : models) {
                            availableModels.add(model.getLanguage());
                        }
                    })
            .addOnFailureListener(
                    e -> {
                        // Model downloading failed.
                        // ...
                        Log.d(TAG, "onFailure: Model downloading failed.");
                    });
    return availableModels;
}

I would really appreciate any help and advice.

You can create a TranslateRemoteModel for the specific language and then use RemoteModelManager#isModelDownloaded API to check. https://developers.google.com/android/reference/com/google/mlkit/common/model/RemoteModelManager#isModelDownloaded(com.google.mlkit.common.model.RemoteModel)

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