hai每一個。我是Android新手。在我的項目中,我閱讀XML文件時遇到了一些問題。在我的xml中,我包含了一些音頻和視頻路徑,並且我想通過代碼讀取xml文件,並且如果有一些音頻或視頻文件,我想在我的視圖中顯示一些圖像。可以在任何機構的電話如何閱讀XML文件。 請多關照檢查xml中的資源並顯示android中的資源的圖標
0
A
回答
1
好首先你需要創建一個解析器下面是代碼來做到這一點:
公共靜態無效readTemplateFile(上下文的背景下){
/**
Include File Checking
*/
try {
XML_Handler_Template myExHan = new XML_Handler_Template();
InputStreamReader isr = new
FileReader(new File(Environment.getExternalStorageDirectory().getPath() + "/Library Template.xml"));
XML_Handler_Template.context = context;
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler((ContentHandler) myExHan);
xr.parse(new InputSource(isr));
} catch (Exception e) {
Toast.makeText(context, ">" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
然後,您需要一個處理程序類。在上面的例子中,我的類被稱爲XML_Handler_Template。 (新文件(Environment.getExternalStorageDirectory()。getPath()+「/FILEPATH/FILE.XML」));}};}};}}
這裏是此刻卻是空白的XML_Handler_Class:
進口org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler;
公共類XML_Handler_Template擴展了DefaultHandler {
public static Context context = null;
@Override
public void startDocument() throws SAXException {
//this is called when the document is first read
}
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
//This is called when a new Tag is opened
//localName holds the Tag Name, the Value is got from the
//Characters function at the end of this class
//the attributes for each tag are stored in the atts array, you can either handle the attribute values here or pass the information to a separate function to handle them,
if (atts.getLength()>0){
for (int i=0;i<atts.getLength();i++){
addAttrib(atts.getLocalName(i) , atts.getValue(i)) ;
}
}
}
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
//This is called when a Tag is closed
}
@Override
public void endDocument() throws SAXException {
//this is called when the document is closed
}
@Override
public void characters(char ch[], int start, int length) {
//This is where the value of a Tag are read
String value = new String(ch , start , length);
// You may want to include a replaceAll("\r","") and replaceAll("\n","") to remove any hidden chars
}
}
有這樣的發揮,看看你的身體情況如何,現在= 0),我通過上下文的類,所以雖然我是學我可以用敬酒向我展示正在閱讀的價值。
相關問題
- 1. 如何檢查Android資源資源?
- 2. Android - 從資源drawable中顯示圖像
- 3. 資源內置Android資源中的$ NotFoundException?
- 4. 資源檢查
- 5. 檢查是否顯示數組中的資源ID並點擊它(Android-Espresso)
- 6. xml資源中的警告
- 7. Android字符串資源中的XML?
- 8. Android中的位圖:用於顯示爲50dp的資源大小
- 9. 的Android查找資源
- 10. 在Java Swing中顯示資源圖像
- 11. 在web資源中顯示url圖像
- 12. 的Android資源$ NotFoundException:資源ID#0x7f020052
- 13. Android:Howto獲取Android資源的資源ID
- 14. Xamarin.Forms Android資源$ NotFoundException對於可繪製的xml資源
- 15. WPF:GroupBox標題中的資源
- 16. 顯示資源問題的圖像
- 17. 如何從XML資源中的矢量圖像檢索位圖
- 18. 從nl.siegmann.epublib獲取資源的位圖資源加載資源
- 19. WinForms中的圖像資源
- 20. 如何檢查Azure資源的資源刪除操作結果
- 21. 資源管理器不顯示XML
- 22. 的Android資源ID
- 23. 查詢Qt資源集合(.qrc)中的資源數量
- 24. 顯示資源地圖圖像
- 25. Android:檢查背景資源可繪製
- 26. 如何檢查一個XML文件中的資源的Android工作室
- 27. 從Android的原始資源獲取Xml
- 28. Asp.net中的資源
- 29. 檢查OpenGL資源泄漏
- 30. 檢查/比較imagebutton資源
k謝謝。我有存儲在SD卡上,我想顯示相關的音頻\視頻圖標。 – user493244 2010-12-21 08:11:28
好吧有幾件事情需要實施 – 2010-12-21 20:36:58
好吧,有很多事情需要你去處理。 1. XML解析 2.可繪圖(不同設備需要的圖標和規格) 3.視圖和充氣器。添加自定義ListItems到列表視圖或動態添加視圖到ScrollView 你是否熟悉上述任何一個? – 2010-12-21 20:47:21