我在资产文件夹assets / pictures / file.png上有图像文件,我试图将这些文件复制到SD卡。但是我得到错误无法复制资产文件:“+”pictures /“+ name
这是我用来读取和复制资产文件到SD卡的代码
String[] getImagesFromAssets() {
AssetManager assetManager = getAssets();
String[] img_files = null;
try {
// img_files = getAssets().list("pictures");
img_files = assetManager.list("pictures");
} catch (IOException ex) {
Logger.getLogger(GameActivity.class
.getName()).log(Level.SEVERE, null, ex);
}
return img_files;
}
void loadImage(String name) {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("pictures/" + name);
File outFile = new File(getExternalFilesDir(null)+"/"+"pictures/"+ name);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
Bitmap myBitmap = BitmapFactory.decodeFile(outFile.getAbsolutePath());
image.setImageBitmap(myBitmap);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + "pictures/" + name, e);
return;
}
}
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);
}
}
更新:Logcat
01-29 19:39:49.959: I/System.out(12042): winzip.png
01-29 19:39:49.964: I/System.out(12042): vimeo.png
01-29 19:39:49.964: I/System.out(12042): yahoo.png
01-29 19:39:49.964: I/System.out(12042): visa.png
01-29 19:39:49.964: I/System.out(12042): winrar.png
01-29 19:39:49.964: I/System.out(12042): ati.png
01-29 19:39:49.964: I/System.out(12042): benq.png
01-29 19:39:49.964: I/System.out(12042): audi.png
01-29 19:39:49.964: I/System.out(12042): acer.png
01-29 19:39:49.964: I/System.out(12042): wikipedia.png
01-29 19:39:49.964: I/System.out(12042): amd.png
01-29 19:39:49.964: I/System.out(12042): wikimedia.png
01-29 19:39:49.964: I/System.out(12042): aol.png
01-29 19:39:49.964: I/System.out(12042): yamaha.png
01-29 19:39:49.964: I/System.out(12042): vaio.png
01-29 19:39:49.964: I/System.out(12042): wordpress.png
01-29 19:39:49.964: W/Ads(12042): loadAd called while the ad is already loading, so aborting.
01-29 19:39:49.964: E/tag(12042): Failed to copy asset file: pictures/winzip.png
01-29 19:39:49.964: E/tag(12042): java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.appsnthemes.spotthebrand/files/pictures/winzip.png: open failed: ENOENT (No such file or directory)
01-29 19:39:49.964: E/tag(12042): at libcore.io.IoBridge.open(IoBridge.java:409)
01-29 19:39:49.964: E/tag(12042): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-29 19:39:49.964: E/tag(12042): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-29 19:39:49.964: E/tag(12042): at com.appsnthemes.spotthebrand.GameActivity.loadImage(GameActivity.java:589)
01-29 19:39:49.964: E/tag(12042): at com.appsnthemes.spotthebrand.GameActivity.onCreate(GameActivity.java:191)
01-29 19:39:49.964: E/tag(12042): at android.app.Activity.performCreate(Activity.java:5369)
01-29 19:39:49.964: E/tag(12042): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
01-29 19:39:49.964: E/tag(12042): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
01-29 19:39:49.964: E/tag(12042): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
01-29 19:39:49.964: E/tag(12042): at android.app.ActivityThread.access$700(ActivityThread.java:165)
01-29 19:39:49.964: E/tag(12042): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
01-29 19:39:49.964: E/tag(12042): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 19:39:49.964: E/tag(12042): at android.os.Looper.loop(Looper.java:137)
01-29 19:39:49.964: E/tag(12042): at android.app.ActivityThread.main(ActivityThread.java:5450)
01-29 19:39:49.964: E/tag(12042): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 19:39:49.964: E/tag(12042): at java.lang.reflect.Method.invoke(Method.java:525)
01-29 19:39:49.964: E/tag(12042): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-29 19:39:49.964: E/tag(12042): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-29 19:39:49.964: E/tag(12042): at dalvik.system.NativeStart.main(Native Method)
01-29 19:39:49.964: E/tag(12042): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
01-29 19:39:49.964: E/tag(12042): at libcore.io.Posix.open(Native Method)
01-29 19:39:49.964: E/tag(12042): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-29 19:39:49.964: E/tag(12042): at libcore.io.IoBridge.open(IoBridge.java:393)
01-29 19:39:49.964: E/tag(12042): ... 18 more