Skip to content

Commit 6b4f1ea

Browse files
committed
feat: improve logic for cp assets
1 parent 4fe7208 commit 6b4f1ea

5 files changed

Lines changed: 125 additions & 8 deletions

File tree

Android/APIExample/app/src/main/java/io/agora/api/example/examples/advanced/beauty/ByteDanceBeauty.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.view.View;
77
import android.view.ViewGroup;
88
import android.view.ViewParent;
9+
import android.widget.Toast;
910

1011
import androidx.annotation.NonNull;
1112
import androidx.annotation.Nullable;
@@ -82,7 +83,15 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
8283
ByteDanceBeautySDK.INSTANCE.getRenderManager(),
8384
new EventCallback(beautyStats -> null,
8485
() -> {
85-
ByteDanceBeautySDK.INSTANCE.initEffect(requireContext());
86+
boolean authSuccess = ByteDanceBeautySDK.INSTANCE.initEffect(requireContext());
87+
if(!authSuccess){
88+
runOnUIThread(new Runnable() {
89+
@Override
90+
public void run() {
91+
Toast.makeText(getContext(), "auth failed", Toast.LENGTH_SHORT).show();
92+
}
93+
});
94+
}
8695
return null;
8796
},
8897
() -> {

Android/APIExample/app/src/main/java/io/agora/api/example/examples/advanced/beauty/ByteDanceBeautySDK.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package io.agora.api.example.examples.advanced.beauty
33
import android.content.Context
44
import android.util.Log
55
import com.effectsar.labcv.effectsdk.RenderManager
6-
import io.agora.api.example.utils.FileUtils
6+
import io.agora.api.example.examples.advanced.beauty.utils.FileUtils
77
import io.agora.beautyapi.bytedance.ByteDanceBeautyAPI
88
import java.io.File
99

Android/APIExample/app/src/main/java/io/agora/api/example/examples/advanced/beauty/SenseTimeBeautySDK.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.softsugar.stmobile.STMobileEffectNative
88
import com.softsugar.stmobile.STMobileEffectParams
99
import com.softsugar.stmobile.STMobileHumanActionNative
1010
import com.softsugar.stmobile.params.STEffectBeautyType
11-
import io.agora.api.example.utils.FileUtils
11+
import io.agora.api.example.examples.advanced.beauty.utils.FileUtils
1212
import io.agora.beautyapi.sensetime.SenseTimeBeautyAPI
1313

1414
object SenseTimeBeautySDK {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2023 Agora Community
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package io.agora.api.example.examples.advanced.beauty.utils
26+
27+
import android.content.Context
28+
import android.util.Log
29+
import java.io.BufferedInputStream
30+
import java.io.BufferedOutputStream
31+
import java.io.BufferedReader
32+
import java.io.File
33+
import java.io.FileOutputStream
34+
import java.io.IOException
35+
import java.io.InputStream
36+
import java.io.InputStreamReader
37+
import java.io.OutputStream
38+
39+
object FileUtils {
40+
val TAG = "FileUtils"
41+
42+
fun getAssetsString(context: Context, path: String): String {
43+
val sb = StringBuilder()
44+
var isr: InputStreamReader? = null
45+
var br: BufferedReader? = null
46+
try {
47+
isr = InputStreamReader(context.resources.assets.open(path))
48+
br = BufferedReader(isr)
49+
var line: String? = null
50+
while (br.readLine().also { line = it } != null) {
51+
sb.append(line).append("\n")
52+
}
53+
} catch (e: IOException) {
54+
Log.e(TAG, "getAssetsString error: $e")
55+
} finally {
56+
if (isr != null) {
57+
try {
58+
isr.close()
59+
} catch (e: IOException) {
60+
e.printStackTrace()
61+
}
62+
}
63+
if (br != null) {
64+
try {
65+
br.close()
66+
} catch (e: IOException) {
67+
e.printStackTrace()
68+
}
69+
}
70+
}
71+
return sb.toString()
72+
}
73+
74+
fun copyAssets(context: Context, assetsPath: String, targetPath: String) {
75+
val fileNames = context.resources.assets.list(assetsPath)
76+
if (fileNames?.isNotEmpty() == true) {
77+
val targetFile = File(targetPath)
78+
if (!targetFile.exists() && !targetFile.mkdirs()) {
79+
return
80+
}
81+
for (fileName in fileNames) {
82+
copyAssets(
83+
context,
84+
"$assetsPath/$fileName",
85+
"$targetPath/$fileName"
86+
)
87+
}
88+
} else {
89+
copyAssetsFile(context, assetsPath, targetPath)
90+
}
91+
}
92+
93+
private fun copyAssetsFile(context: Context, assetsFile: String, targetPath: String) {
94+
val dest = File(targetPath)
95+
dest.parentFile?.mkdirs()
96+
var input: InputStream? = null
97+
var output: OutputStream? = null
98+
try {
99+
input = BufferedInputStream(context.assets.open(assetsFile))
100+
output = BufferedOutputStream(FileOutputStream(dest))
101+
val buffer = ByteArray(1024)
102+
var length = 0
103+
while (input.read(buffer).also { length = it } != -1) {
104+
output.write(buffer, 0, length)
105+
}
106+
} catch (e: Exception) {
107+
Log.e(TAG, "copyAssetsFile", e)
108+
} finally {
109+
output?.close()
110+
input?.close()
111+
}
112+
}
113+
}

Android/APIExample/app/src/main/java/io/agora/api/example/utils/FileUtils.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ public static void copyFilesFromAssets(Context context, String assetsPath, Strin
8686

8787
}
8888

89-
public static void copyAssets(Context context, String assetsPath, String storagePath) {
90-
copyFilesFromAssets(context, assetsPath, storagePath);
91-
}
92-
93-
9489
/**
9590
* 读取输入流中的数据写入输出流
9691
*

0 commit comments

Comments
 (0)