2013-07-16 35 views
0

在我的Android应用我已经打包在需要复制到的SD卡的/ assets文件夹中的文件。当我列出的资产文件夹的内容到String[]我得到“图像”,“声音”,“WebKit的”和“kioskmode”但不是我的文件手动添加到资产的文件夹。为什么AssetManager list()不显示我的资产文件夹?

我的代码是在这里:

private void copyAsset() { 
    AssetManager am = getApplicationContext().getAssets(); 
    String[] files = null; 
    try { 
     files = am.list(""); 
    } catch (IOException e) { 

    } 

    for (String filename : files) { 
     if (filename.equals("images") || filename.equals("kioskmode") || 
       filename.equals("sounds") || filename.equals("webkit")) { 
      Log.i(TAG, "Skipping folder " + filename); 
      continue; 
     } 
     InputStream in = null; 
     OutputStream out = null; 
     try { 
      in = am.open(filename); 
      File outFile = new File(Environment.getExternalStorageDirectory().getPath(), filename); 
      out = new FileOutputStream(outFile); 
      copyFile(in, out); 
      in.close(); 
      in = null; 
      out.flush(); 
      out.close(); 
      out = null; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Log.e(TAG, "Error copying asset ", e); 
     } 
    } 
} 

是否有所作为,这是第二类在我的应用程序,被称为使用Intent showHelper = new Intent(this, HelperManager.class); startActivity(showHelper);我的MainActivity?

我曾尝试使用和不使用getApplicationContext()位的第二线(AssetManager am = ...),试图将文件移动到/资产的子目录,并试图用files = am.list("")前缘和后斜线。如果我使用一个子文件夹中的文件数组为空当代码运行(在files = am.list("");行设置断点,并检查它在运行时 奇怪的是,它的工作一次 - 当我第一次写的代码,但进一步测试,我删除了手机上的/ SD卡文件夹中的文件,因为即使文件仍处于资产文件夹它从来没有工作过。 我使用过Android Studio,如果该事项。 感谢

+0

你有什么东西吗?资产文件夹里面? – Blackbelt

+0

是的,我做@blackbelt如果我浏览到项目位置,我可以在Android Studio的项目布局中以及在Windows资源管理器中看到它。也许我会尝试删除并重新添加文件(它是一个.apk btw)。 – Mark

+0

如果这些文件与apk版捆绑在一起,那你就很好。你了解'continue'关键字的含义吗?你想要的“图像”,“声音”,“WebKit的”和“kioskmode内部SD卡写入? – Blackbelt

回答

0

设法得到使用Load a simple text file in Android Studio的修复方案。它仍然放4个文件夹中的文件数组中,但我可以使用代码跳过它们上面给出的,虽然我倒是应该检查我想我做的不是文件,而不是4!

相关问题