-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
App/Activity: Let the FileProvider load bundled model files
This patch makes the ModelFileProvider pre-load the model files, which are packaged into the APK file. According to this change, a test case for verifyng the number of models preloaded has also been updated. Signed-off-by: Wook Song <[email protected]>
- Loading branch information
Showing
4 changed files
with
102 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...oading/src/main/java/ai/nnstreamer/ml/inference/offloading/providers/ModelFileProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ai.nnstreamer.ml.inference.offloading.providers | ||
|
||
import ai.nnstreamer.ml.inference.offloading.R | ||
import android.content.Context | ||
import androidx.core.content.FileProvider | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
|
||
class ModelFileProvider : FileProvider(R.xml.file_paths) { | ||
companion object { | ||
val assetPaths = listOf("models") | ||
} | ||
|
||
private fun copyAssetsToExternal(ctx: Context, path: String) { | ||
val resAssets = ctx.resources.assets | ||
val externalDir = ctx.getExternalFilesDir(null)?.resolve(path) | ||
?: throw IOException("Failed to resolve $path in the App's private external storage") | ||
|
||
try { | ||
if (!externalDir.isDirectory && !externalDir.mkdir()) { | ||
throw IOException("Failed to create $externalDir") | ||
} | ||
} catch (e: SecurityException) { | ||
val msg = e.message ?: "none" | ||
|
||
throw IOException("Failed to access $externalDir for the security reason (details: $msg)") | ||
} | ||
|
||
|
||
resAssets.list(path)?.run { | ||
filterNotNull().onEach { file -> | ||
val os = FileOutputStream(externalDir.resolve(file)) | ||
|
||
resAssets.open("$path/$file").use { stream -> | ||
stream.copyTo(os) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
override fun onCreate(): Boolean { | ||
context?.run { | ||
assetPaths.onEach { path -> | ||
try { | ||
copyAssetsToExternal(this, path) | ||
} catch (e: Exception) { | ||
throw RuntimeException("Failed to preload $path in the APK's assets directory") | ||
} | ||
} | ||
} | ||
|
||
return super.onCreate() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters