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. android中的圖像資源
- 5. 資源檢查
- 6. 檢查背景資源Android
- 7. 檢查是否顯示數組中的資源ID並點擊它(Android-Espresso)
- 8. xml資源中的警告
- 9. Android庫中的XML資源衝突
- 10. Android字符串資源中的XML?
- 11. 的Android - 檢索資源
- 12. php:顯示圖像資源
- 13. 未顯示圖像資源
- 14. 嵌套的資源檢查
- 15. Android中的位圖:用於顯示爲50dp的資源大小
- 16. 的Android查找資源
- 17. Android:Howto獲取Android資源的資源ID
- 18. 的Android資源$ NotFoundException:資源ID#0x7f020052
- 19. 在web資源中顯示url圖像
- 20. 在Java Swing中顯示資源圖像
- 21. Android:如何顯示只有資源整數ID的圖標?
- 22. 在其父資源視圖中顯示嵌套資源的錯誤消息
- 23. Xamarin.Forms Android資源$ NotFoundException對於可繪製的xml資源
- 24. JAR中的Android庫資源
- 25. Android中的資源連接
- 26. WPF:GroupBox標題中的資源
- 27. eclipse中的資源ID沒有顯示
- 28. 事件中的顯示資源名稱
- 29. Android獲取資源文件夾以顯示資源類型
- 30. 從C窗體窗口中的本地資源顯示圖標#
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