Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
王雷
/
mnist
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit fa8b4a52
authored
Apr 29, 2024
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新读取Assets文件方法
1 parent
c1cc7883
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
8 deletions
app/src/main/java/com/agenew/mnist/Utils.java
app/src/main/java/com/agenew/mnist/Utils.java
View file @
fa8b4a5
...
...
@@ -2,7 +2,7 @@ package com.agenew.mnist;
import
android.content.Context
;
import
android.content.res.AssetFileDescriptor
;
import
android.
content.res.AssetManager
;
import
android.
util.Log
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
...
...
@@ -10,16 +10,51 @@ import java.nio.MappedByteBuffer;
import
java.nio.channels.FileChannel
;
public
class
Utils
{
private
static
final
String
TAG
=
"Utils"
;
public
static
final
int
PIXEL_SIZE
=
28
;
private
static
final
String
MODEL_FILE
=
"mnist.tflite"
;
public
static
MappedByteBuffer
loadModelFile
(
Context
context
)
throws
IOException
{
AssetManager
assets
=
context
.
getAssets
();
AssetFileDescriptor
fileDescriptor
=
assets
.
openFd
(
MODEL_FILE
);
FileInputStream
inputStream
=
new
FileInputStream
(
fileDescriptor
.
getFileDescriptor
());
FileChannel
fileChannel
=
inputStream
.
getChannel
();
long
startOffset
=
fileDescriptor
.
getStartOffset
();
long
declaredLength
=
fileDescriptor
.
getDeclaredLength
();
return
fileChannel
.
map
(
FileChannel
.
MapMode
.
READ_ONLY
,
startOffset
,
declaredLength
);
return
loadMappedFile
(
context
,
MODEL_FILE
);
}
public
static
MappedByteBuffer
loadMappedFile
(
Context
context
,
String
filePath
)
throws
IOException
{
AssetFileDescriptor
fileDescriptor
=
context
.
getAssets
().
openFd
(
filePath
);
MappedByteBuffer
result
=
null
;
try
{
FileInputStream
inputStream
=
new
FileInputStream
(
fileDescriptor
.
getFileDescriptor
());
try
{
FileChannel
fileChannel
=
inputStream
.
getChannel
();
long
startOffset
=
fileDescriptor
.
getStartOffset
();
long
declaredLength
=
fileDescriptor
.
getDeclaredLength
();
result
=
fileChannel
.
map
(
FileChannel
.
MapMode
.
READ_ONLY
,
startOffset
,
declaredLength
);
}
catch
(
Throwable
e
)
{
try
{
inputStream
.
close
();
}
catch
(
Throwable
e1
)
{
e
.
addSuppressed
(
e1
);
}
throw
e
;
}
finally
{
inputStream
.
close
();
}
}
catch
(
Throwable
e
)
{
if
(
fileDescriptor
!=
null
)
{
try
{
fileDescriptor
.
close
();
}
catch
(
Throwable
e1
)
{
e
.
addSuppressed
(
e1
);
}
}
Log
.
e
(
TAG
,
"WL_DEBUG loadMappedFile e = "
+
e
,
e
);
}
finally
{
if
(
fileDescriptor
!=
null
)
{
fileDescriptor
.
close
();
}
}
return
result
;
}
}
Write
Preview
Markdown
is supported
Attach a file
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 post a comment