Read xml file from Android Asset folder

I am trying to read the same file "xmlfile.xml" from the assets folder, as well as another copy from the sdcard / download / sd card.

I can read from the SD card:

  • unfile Return True
  • Esite return true

I can not read from the Properties folder:

  • unfile Return False
  • Esite Return False

This code does not work

File source = new File("file:///android_asset/xmlfile.xml"); boolean unfile = source.isFile(); boolean Esiste = source.exists(); try { // todo } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } 

This code is il Working

  File source = new File("/sdcard/" + "download" + "/" + "xmlfile.xml"); boolean unfile = source.isFile(); boolean Esiste = source.exists(); try { // todo } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } 

can someone explain to me how I can read a file from the Assets folder.

thanks marco

+8
android xml android-assets
source share
4 answers

To open an asset, you will need the following code fragment:

 InputStream is = getAssets().open("xmlfile.xml") 
+11
source share

Use this function

 // Converting given XML file name to String form String mOutputText = getxml("yourxml.xml"); /** * Function used to fetch an XML file from assets folder * @param fileName - XML file name to convert it to String * @return - return XML in String form */ private String getXml(String fileName) { String xmlString = null; AssetManager am = context.getAssets(); try { InputStream is = am.open(fileName); int length = is.available(); byte[] data = new byte[length]; is.read(data); xmlString = new String(data); } catch (IOException e1) { e1.printStackTrace(); } return xmlString; } 
+6
source share
  try { AssetManager aManager = Class.getAssets(); InputStream iStream = aManager.open("file.xml"); int length = iStream.available(); byte[] data = new byte[length]; iStream.read(data); assetString = new String(data).toString(); } catch (IOException e) { e.printStackTrace(); } 
0
source share

This will help you. To read a folder with a file form, you need an InputStream object.

Syntax:

 InputStream yourobj=getApplicationContext().getAssets().open("Path to xml file"); 

Thus, real-time code can be:

 InputStream in_s = getApplicationContext().getAssets().open("www/application/app/client/controllers/data1.xml"); 

Here data1.xml is the file inside the path.

0
source share

All Articles