Skip to content

Commit 9692b75

Browse files
committed
add try catch.
1 parent 5457b0a commit 9692b75

File tree

5 files changed

+77
-65
lines changed

5 files changed

+77
-65
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
android:theme="@style/AppTheme"
1010
android:hardwareAccelerated="true" >
1111
<activity
12-
android:name=".SimpleActivity"
12+
android:name=".MainActivity"
1313
android:label="@string/app_name" >
1414
<intent-filter>
1515
<action android:name="android.intent.action.MAIN" />

app/src/main/java/com/example/ponycui_home/svgaplayer/MainActivity.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5656
testView = new SVGAImageView(this);
5757
testView.setBackgroundColor(Color.GRAY);
5858
setupCallback();
59-
loadDynamicText();
60-
loadDynamicBitmap(new Runnable() {
61-
@Override
62-
public void run() {
63-
loadAnimation();
64-
}
65-
});
59+
loadAnimation();
6660
setContentView(testView);
6761
}
6862

@@ -160,7 +154,9 @@ public void onError() {
160154

161155
}
162156
});
163-
} catch (Exception e) {}
157+
} catch (Exception e) {
158+
System.out.print(true);
159+
}
164160
}
165161

166162
}

library/src/main/java/com/opensource/svgaplayer/SVGAParser.kt

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,31 @@ class SVGAParser(val context: Context) {
3131
}
3232

3333
fun parse(assetsName: String): SVGAVideoEntity? {
34-
context.assets.open(assetsName)?.let {
35-
return parse(it, cacheKey("file:///assets/" + assetsName))
34+
try {
35+
context.assets.open(assetsName)?.let {
36+
return parse(it, cacheKey("file:///assets/" + assetsName))
37+
}
38+
} catch (e: Exception) {
39+
return null
3640
}
3741
return null
3842
}
3943

4044
fun parse(url: URL): SVGAVideoEntity? {
41-
if (cacheDir(cacheKey(url)).exists()) {
42-
return parse(null, cacheKey(url))
43-
}
44-
else {
45-
(url.openConnection() as? HttpURLConnection)?.let {
46-
it.connectTimeout = 20 * 1000
47-
it.requestMethod = "GET"
48-
it.connect()
49-
return parse(it.inputStream, cacheKey(url))
45+
try {
46+
if (cacheDir(cacheKey(url)).exists()) {
47+
return parse(null, cacheKey(url))
48+
}
49+
else {
50+
(url.openConnection() as? HttpURLConnection)?.let {
51+
it.connectTimeout = 20 * 1000
52+
it.requestMethod = "GET"
53+
it.connect()
54+
return parse(it.inputStream, cacheKey(url))
55+
}
5056
}
57+
} catch (e: Exception) {
58+
return null
5159
}
5260
return null
5361
}
@@ -74,26 +82,30 @@ class SVGAParser(val context: Context) {
7482
}
7583
val cacheDir = File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
7684
val jsonFile = File(cacheDir, "movie.spec")
77-
FileInputStream(jsonFile)?.let {
78-
val fileInputStream = it
79-
val byteArrayOutputStream = ByteArrayOutputStream()
80-
val buffer = ByteArray(2048)
81-
while (true) {
82-
val size = fileInputStream.read(buffer, 0, buffer.size)
83-
if (size == -1) {
84-
break
85+
try {
86+
FileInputStream(jsonFile)?.let {
87+
val fileInputStream = it
88+
val byteArrayOutputStream = ByteArrayOutputStream()
89+
val buffer = ByteArray(2048)
90+
while (true) {
91+
val size = fileInputStream.read(buffer, 0, buffer.size)
92+
if (size == -1) {
93+
break
94+
}
95+
byteArrayOutputStream.write(buffer, 0, size)
8596
}
86-
byteArrayOutputStream.write(buffer, 0, size)
87-
}
88-
byteArrayOutputStream.toString()?.let {
89-
JSONObject(it)?.let {
90-
fileInputStream.close()
91-
return SVGAVideoEntity(it, cacheDir)
97+
byteArrayOutputStream.toString()?.let {
98+
JSONObject(it)?.let {
99+
fileInputStream.close()
100+
return SVGAVideoEntity(it, cacheDir)
101+
}
92102
}
93103
}
104+
cacheDir.delete()
105+
jsonFile.delete()
106+
} catch (e: Exception) {
107+
return null
94108
}
95-
cacheDir.delete()
96-
jsonFile.delete()
97109
return null
98110
}
99111

@@ -117,25 +129,27 @@ class SVGAParser(val context: Context) {
117129
}
118130

119131
private fun unzip(inputStream: InputStream, cacheKey: String) {
120-
val cacheDir = this.cacheDir(cacheKey)
121-
cacheDir.mkdirs()
122-
val zipInputStream = ZipInputStream(BufferedInputStream(inputStream))
123-
while (true) {
124-
val zipItem = zipInputStream.nextEntry ?: break
125-
val file = File(cacheDir, zipItem.name)
126-
val fileOutputStream = FileOutputStream(file)
127-
val buff = ByteArray(2048)
132+
try {
133+
val cacheDir = this.cacheDir(cacheKey)
134+
cacheDir.mkdirs()
135+
val zipInputStream = ZipInputStream(BufferedInputStream(inputStream))
128136
while (true) {
129-
val readBytes = zipInputStream.read(buff)
130-
if (readBytes <= 0) {
131-
break
137+
val zipItem = zipInputStream.nextEntry ?: break
138+
val file = File(cacheDir, zipItem.name)
139+
val fileOutputStream = FileOutputStream(file)
140+
val buff = ByteArray(2048)
141+
while (true) {
142+
val readBytes = zipInputStream.read(buff)
143+
if (readBytes <= 0) {
144+
break
145+
}
146+
fileOutputStream.write(buff, 0, readBytes)
132147
}
133-
fileOutputStream.write(buff, 0, readBytes)
148+
fileOutputStream.close()
149+
zipInputStream.closeEntry()
134150
}
135-
fileOutputStream.close()
136-
zipInputStream.closeEntry()
137-
}
138-
zipInputStream.close()
151+
zipInputStream.close()
152+
} catch (e: Exception) { }
139153
}
140154

141155
}

library/src/main/java/com/opensource/svgaplayer/SVGAVideoEntity.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class SVGAVideoEntity(obj: JSONObject, val cacheDir: File) {
3636
private set
3737

3838
init {
39-
obj.getJSONObject("movie")?.let {
40-
it.getJSONObject("viewBox")?.let {
39+
obj.optJSONObject("movie")?.let {
40+
it.optJSONObject("viewBox")?.let {
4141
videoSize = SVGARect(0.0, 0.0, it.optDouble("width", 0.0), it.optDouble("height", 0.0))
4242
}
4343
FPS = it.optInt("fps", 20)
@@ -48,7 +48,7 @@ class SVGAVideoEntity(obj: JSONObject, val cacheDir: File) {
4848
}
4949

5050
internal fun resetImages(obj: JSONObject) {
51-
obj.getJSONObject("images")?.let {
51+
obj.optJSONObject("images")?.let {
5252
val imgObjects = it
5353
it.keys().forEach {
5454
val imageKey = it
@@ -70,9 +70,9 @@ class SVGAVideoEntity(obj: JSONObject, val cacheDir: File) {
7070

7171
internal fun resetSprites(obj: JSONObject) {
7272
val mutableList: MutableList<SVGAVideoSpriteEntity> = mutableListOf()
73-
obj.getJSONArray("sprites")?.let {
73+
obj.optJSONArray("sprites")?.let {
7474
for (i in 0..it.length() - 1) {
75-
it.getJSONObject(i)?.let {
75+
it.optJSONObject(i)?.let {
7676
mutableList.add(SVGAVideoSpriteEntity(it))
7777
}
7878
}

library/src/main/java/com/opensource/svgaplayer/SVGAVideoSpriteEntity.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@ import java.util.ArrayList
1212
*/
1313
class SVGAVideoSpriteEntity(obj: JSONObject) {
1414

15-
val imageKey: String? = obj.getString("imageKey")
15+
val imageKey: String? = obj.optString("imageKey")
1616

1717
val frames: List<SVGAVideoSpriteFrameEntity>
1818

1919
init {
2020
val mutableFrames: MutableList<SVGAVideoSpriteFrameEntity> = mutableListOf()
21-
obj.getJSONArray("frames")?.let {
21+
obj.optJSONArray("frames")?.let {
2222
for (i in 0..it.length() - 1) {
23-
val frameItem = SVGAVideoSpriteFrameEntity(it.getJSONObject(i))
24-
if (frameItem.shapes.size > 0) {
25-
frameItem.shapes.first()?.let {
26-
if (it.isKeep && mutableFrames.size > 0) {
27-
frameItem.shapes = mutableFrames.last().shapes
23+
it.optJSONObject(i)?.let {
24+
val frameItem = SVGAVideoSpriteFrameEntity(it)
25+
if (frameItem.shapes.size > 0) {
26+
frameItem.shapes.first()?.let {
27+
if (it.isKeep && mutableFrames.size > 0) {
28+
frameItem.shapes = mutableFrames.last().shapes
29+
}
2830
}
2931
}
32+
mutableFrames.add(frameItem)
3033
}
31-
mutableFrames.add(frameItem)
3234
}
3335
}
3436
frames = mutableFrames.toList()

0 commit comments

Comments
 (0)