Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
YGOMobile
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fallenstardust
YGOMobile
Commits
226796ec
Commit
226796ec
authored
Sep 28, 2021
by
qq247321453
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
下载文件大小检测
parent
c9e97ac8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
67 deletions
+80
-67
mobile/src/main/java/cn/garymb/ygomobile/ui/cards/CardDetail.java
...rc/main/java/cn/garymb/ygomobile/ui/cards/CardDetail.java
+2
-0
mobile/src/main/java/cn/garymb/ygomobile/utils/DownloadUtil.java
...src/main/java/cn/garymb/ygomobile/utils/DownloadUtil.java
+78
-67
No files found.
mobile/src/main/java/cn/garymb/ygomobile/ui/cards/CardDetail.java
View file @
226796ec
...
@@ -5,6 +5,7 @@ import android.content.Intent;
...
@@ -5,6 +5,7 @@ import android.content.Intent;
import
android.os.Handler
;
import
android.os.Handler
;
import
android.os.Message
;
import
android.os.Message
;
import
android.text.TextUtils
;
import
android.text.TextUtils
;
import
android.util.Log
;
import
android.view.View
;
import
android.view.View
;
import
android.view.ViewGroup
;
import
android.view.ViewGroup
;
import
android.view.Window
;
import
android.view.Window
;
...
@@ -471,6 +472,7 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
...
@@ -471,6 +472,7 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
@Override
@Override
public
void
onDownloadFailed
(
Exception
e
)
{
public
void
onDownloadFailed
(
Exception
e
)
{
Log
.
w
(
IrrlichtBridge
.
TAG
,
"download image error:"
+
e
.
getMessage
());
//下载失败后删除下载的文件
//下载失败后删除下载的文件
FileUtils
.
deleteFile
(
tmp
);
FileUtils
.
deleteFile
(
tmp
);
// downloadCardImage(code, file);
// downloadCardImage(code, file);
...
...
mobile/src/main/java/cn/garymb/ygomobile/utils/DownloadUtil.java
View file @
226796ec
...
@@ -21,7 +21,7 @@ public class DownloadUtil {
...
@@ -21,7 +21,7 @@ public class DownloadUtil {
private
static
DownloadUtil
downloadUtil
;
private
static
DownloadUtil
downloadUtil
;
private
final
OkHttpClient
okHttpClient
;
private
final
OkHttpClient
okHttpClient
;
//暂时关闭
//暂时关闭
private
static
final
boolean
ENABLE_CACHE
=
false
;
private
static
final
boolean
ENABLE_CACHE
=
false
;
private
static
final
Map
<
String
,
Call
>
cache
=
new
HashMap
<>();
private
static
final
Map
<
String
,
Call
>
cache
=
new
HashMap
<>();
public
static
DownloadUtil
get
()
{
public
static
DownloadUtil
get
()
{
...
@@ -44,18 +44,18 @@ public class DownloadUtil {
...
@@ -44,18 +44,18 @@ public class DownloadUtil {
*/
*/
public
void
download
(
final
String
url
,
final
String
destFileDir
,
final
String
destFileName
,
final
OnDownloadListener
listener
)
{
public
void
download
(
final
String
url
,
final
String
destFileDir
,
final
String
destFileName
,
final
OnDownloadListener
listener
)
{
if
(
ENABLE_CACHE
)
{
if
(
ENABLE_CACHE
)
{
synchronized
(
cache
)
{
synchronized
(
cache
)
{
Call
old
=
cache
.
get
(
url
);
Call
old
=
cache
.
get
(
url
);
if
(
old
!=
null
)
{
if
(
old
!=
null
)
{
Log
.
w
(
IrrlichtBridge
.
TAG
,
"exist download task by:"
+
url
);
Log
.
w
(
IrrlichtBridge
.
TAG
,
"exist download task by:"
+
url
);
return
;
return
;
}
}
}
}
}
}
Request
request
=
new
Request
.
Builder
()
Request
request
=
new
Request
.
Builder
()
.
url
(
url
)
.
url
(
url
)
.
build
();
.
build
();
/* OkHttpClient client = new OkHttpClient();
/* OkHttpClient client = new OkHttpClient();
...
@@ -66,66 +66,77 @@ public class DownloadUtil {
...
@@ -66,66 +66,77 @@ public class DownloadUtil {
}*/
}*/
//异步请求
//异步请求
Call
call
=
okHttpClient
.
newCall
(
request
);
Call
call
=
okHttpClient
.
newCall
(
request
);
call
.
enqueue
(
new
Callback
()
{
call
.
enqueue
(
new
Callback
()
{
@Override
@Override
public
void
onFailure
(
Call
call
,
IOException
e
)
{
public
void
onFailure
(
Call
call
,
IOException
e
)
{
// 下载失败监听回调
// 下载失败监听回调
listener
.
onDownloadFailed
(
e
);
listener
.
onDownloadFailed
(
e
);
if
(
ENABLE_CACHE
){
if
(
ENABLE_CACHE
)
{
synchronized
(
cache
){
synchronized
(
cache
)
{
cache
.
remove
(
url
);
cache
.
remove
(
url
);
}
}
}
}
}
}
@Override
@Override
public
void
onResponse
(
Call
call
,
Response
response
)
throws
IOException
{
public
void
onResponse
(
Call
call
,
Response
response
)
throws
IOException
{
InputStream
is
=
null
;
if
(!
response
.
isSuccessful
()){
byte
[]
buf
=
new
byte
[
2048
];
listener
.
onDownloadFailed
(
new
Exception
(
"error:"
+
response
.
code
()));
int
len
=
0
;
return
;
FileOutputStream
fos
=
null
;
}
String
contentLen
=
response
.
header
(
"Content-Length"
);
//储存下载文件的目录
final
long
contentLength
=
(
contentLen
==
null
||
contentLen
.
length
()
==
0
)
?
0
:
Long
.
parseLong
(
contentLen
);
File
dir
=
new
File
(
destFileDir
);
InputStream
is
=
null
;
if
(!
dir
.
exists
())
{
byte
[]
buf
=
new
byte
[
2048
];
dir
.
mkdirs
();
int
len
=
0
;
}
FileOutputStream
out
=
null
;
File
file
=
new
File
(
dir
,
destFileName
);
//储存下载文件的目录
try
{
File
dir
=
new
File
(
destFileDir
);
if
(!
dir
.
exists
())
{
is
=
response
.
body
().
byteStream
();
dir
.
mkdirs
();
long
total
=
response
.
body
().
contentLength
();
}
fos
=
new
FileOutputStream
(
file
);
File
file
=
new
File
(
dir
,
destFileName
);
long
sum
=
0
;
boolean
saved
=
false
;
while
((
len
=
is
.
read
(
buf
))
!=
-
1
)
{
try
{
fos
.
write
(
buf
,
0
,
len
);
is
=
response
.
body
().
byteStream
();
sum
+=
len
;
long
total
=
response
.
body
().
contentLength
();
int
progress
=
(
int
)
(
sum
*
1.0f
/
total
*
100
);
out
=
new
FileOutputStream
(
file
);
//下载中更新进度条
long
sum
=
0
;
listener
.
onDownloading
(
progress
);
while
((
len
=
is
.
read
(
buf
))
!=
-
1
)
{
}
out
.
write
(
buf
,
0
,
len
);
fos
.
flush
();
sum
+=
len
;
//下载完成
int
progress
=
(
int
)
(
sum
*
1.0f
/
total
*
100
);
//下载中更新进度条
listener
.
onDownloading
(
progress
);
}
out
.
flush
();
saved
=
true
;
}
catch
(
Exception
ex
)
{
listener
.
onDownloadFailed
(
ex
);
}
finally
{
IOUtils
.
close
(
out
);
IOUtils
.
close
(
is
);
}
if
(
saved
)
{
if
(
contentLength
>
0
&&
file
.
length
()
<
contentLength
)
{
listener
.
onDownloadFailed
(
new
Exception
(
"file length["
+
file
.
length
()
+
"] < "
+
contentLen
));
}
else
{
listener
.
onDownloadSuccess
(
file
);
listener
.
onDownloadSuccess
(
file
);
}
catch
(
Exception
ex
)
{
listener
.
onDownloadFailed
(
ex
);
}
finally
{
IOUtils
.
close
(
is
);
IOUtils
.
close
(
fos
);
}
if
(
ENABLE_CACHE
){
synchronized
(
cache
){
cache
.
remove
(
url
);
}
}
}
}
}
});
if
(
ENABLE_CACHE
)
{
synchronized
(
cache
)
{
cache
.
remove
(
url
);
}
}
}
});
}
}
public
static
interface
OnDownloadListener
{
public
static
interface
OnDownloadListener
{
/**
/**
* 下载成功之后的文件
* 下载成功之后的文件
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment