java 如何访问我的 android 应用程序中的资产文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10267594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:19:08  来源:igfitidea点击:

How to access assets folder in my android app?

javaandroidandroid-assets

提问by dhartwich

I'm writing at a little project for a friend. A notecard application. My plan is to put the notecards in an xml format so i can import and export them easily. I located the xml files at the assets/xml/mynotecard.xml folder but i just can't manage to get access to this file.

我正在为朋友写一个小项目。记事卡应用程序。我的计划是将记事卡放在 xml 格式中,以便我可以轻松地导入和导出它们。我在 assets/xml/mynotecard.xml 文件夹中找到了 xml 文件,但我无法访问该文件。

Whenever i try to interpret the xml file (will be put to it's on class later) i get the exception with: test.xml is not a file. This is an extract of my code:

每当我尝试解释 xml 文件(稍后将放在课堂上)时,我都会收到异常:test.xml 不是文件。这是我的代码的摘录:

public class NotecardProActivity extends Activity {

公共类 NotecardProActivity 扩展 Activity {

List<String> xmlFiles;
public ArrayList<File> xmlFileList;
XMLInterpreter horst;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   final AssetManager mgr = getAssets();


    displayFiles(mgr, "",0); 
     xmlFiles = displayFiles(mgr, "xml",0); 
     for (int e = 0; e<=xmlFiles.size()-1;e++)
     {
        Log.v("Inhalt List"+e+": ", xmlFiles.get(e));
     }     

    xmlFileList = new ArrayList<File>();
    for (int i = 0; i<=xmlFiles.size()-1;i++)
    {

        xmlFileList.add(new File("./assets/xml/"+xmlFiles.get(i)));
    }
    for (int i = 0; i<=xmlFileList.size()-1;i++)
    {
        Log.v("Name: ", xmlFileList.get(i).getName());
        Log.v("Filelocation: ",xmlFileList.get(i).getPath());
        Log.v("Filelocation: ",xmlFileList.get(i).getAbsolutePath());               
    }
    Log.v("DEBUG","XML FILE LISTE erfolgreich geschrieben!");

    //Alternative zum ausgelagerten XML INTERPRETER
    try
    {
    if(xmlFileList.get(0) == null){
        Log.v("Debug", "XMLFILE IS NULL");
    }
    else{
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFileList.get(0));
    doc.getDocumentElement().normalize();

    Log.v("Root element :", doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("notecard");


    for (int temp = 0; temp < nList.getLength(); temp++) {

           Node nNode = nList.item(temp);
           if (nNode.getNodeType() == Node.ELEMENT_NODE) {

              Element eElement = (Element) nNode;    
              Log.v("GesetzesText: " , getTagValue("legal_text", eElement));
             // System.out.println("Kommentar : " + getTagValue("comment", eElement));                   
           }
        }
    }
    }
    catch (Exception e) {
        e.printStackTrace();
      }

    mgr.close();                
}


private List<String> displayFiles (AssetManager mgr, String path, int level) {
    List<String> abc = new ArrayList<String>();
    Log.v("Hello","enter displayFiles("+path+")");
   try {
       String list[] = mgr.list(path);
       abc.addAll(Arrays.asList(list));
        Log.v("Hello1","L"+level+": list:"+ Arrays.asList(list));

   } catch (IOException e) {
       Log.v("Fail","List error: can't list" + path);
   }
return abc;

}

}

private String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

    Node nValue = (Node) nlList.item(0);

    return nValue.getNodeValue();
}

}

}

Would be really awesome if you guys could help me :)

如果你们能帮助我,那就太棒了:)

回答by Avezou

Zip your xml folder (xml.zip for example) and put it in assets. Getting assets with AssetsManager is very slow. Also, doing so you can maintain your directory structure and just copy it to sdcard and extract it there. This will be a lot faster than getting each file in the directory. You can use this post for the unzipping: How to unzip files programmatically in Android?. Hope this helps.

压缩您的 xml 文件夹(例如 xml.zip)并将其放入资产中。使用 AssetsManager 获取资产非常慢。此外,这样做您可以维护您的目录结构,只需将其复制到 sdcard 并在那里提取。这比获取目录中的每个文件要快得多。您可以使用这篇文章进行解压缩:如何在 Android 中以编程方式解压缩文件?. 希望这可以帮助。

回答by TacB0sS

Assets should be in the root of the folder... cannot put file in a sub folder. assets\file.sufwould work while assets\sub\file.sufwould not

资产应该在文件夹的根目录中...不能将文件放在子文件夹中。 assets\file.suf会工作,而assets\sub\file.suf不会

回答by Shankar Agarwal

android.resource://packagename/assets/filename

you can use the above line to refer to a file in assets folder. Just replace the package name and file name.

您可以使用上面的行来引用资产文件夹中的文件。只需替换包名和文件名即可。

Notefile name must not contain extensions

注意文件名不能包含扩展名

Updated::

更新::

private List<String> getFiles(){
 AssetManager assetManager = getAssets();
    List<String> files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", e.getMessage());
    }
}