我尝试将图片直接从资源复制到应用程序包。这不起作用,抛出异常。如何将图像复制到内部目录?
我尝试了输出路径
Environment.getDataDirectory().toString()+"/facerecogOCV"
和
"data/data/org.opencv.javacv.facerecognition/files/facerecogOCV"
我使用了以下代码:
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("Files");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
System.out.println("Files=>" + filename);
try {
in = assetManager.open("Files=>"+filename);
out = new FileOutputStream(Environment.getDataDirectory().toString()+"files/facerecogOCV");
copyFile(in,out);
in.close();
in=null;
out.flush();
out.close();
out=null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
该异常具有以下堆栈跟踪:
Failed to copy asset file: image-26.jpg
java.io.FileNotFoundException: Files=>image-26.jpg
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:331)
at android.content.res.AssetManager.open(AssetManager.java:305)
at org.opencv.javacv.facerecognition.MainMenu.copyAssets(MainMenu.java:121)
at org.opencv.javacv.facerecognition.MainMenu.onCreate(MainMenu.java:38)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at