简体   繁体   中英

How can I choose from files in local storage instead of from a hardcoded file in res/raw?

I'm trying to write an app that will take an XML file and convert it to HTML using XSLT.

The structure of the XML files will always be the same, but the data inside will change.

I have managed to get it to work using an XML file hardcoded in res/raw.

How can I change the app so that I can choose an XML file from the local storage on the device?

Here is my code:

I have a MainActivity with just a single button that kicks off the Activity that is doing the conversion:

MainActivity

 package com.example.xml_html_webview; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button)findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, LoadXSLTinWebview.class)); } }); } }

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

Activity that does the conversion:

 package com.example.xml_html_webview; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.print.PrintAttributes; import android.print.PrintDocumentAdapter; import android.print.PrintJob; import android.print.PrintManager; import android.util.Log; import android.view.Window; import android.webkit.WebView; import com.example.xml_html_webview.R; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class LoadXSLTinWebview extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_PROGRESS); WebView webview = new WebView(this); setContentView(webview); //Reading XSLT String strXSLT = GetStyleSheet(R.raw.xsltfile); //Reading XML String strXML = GetStyleSheet(R.raw.xmlfile); /* * Loading XSLT... */ //Transform ... String html=StaticTransform(strXSLT, strXML); // HTML button added to transformed XML String html_button= "<html><script type=\\"text/javascript\\">function createWebPrintJob() {\\nAndroid.createWebPrintJob();\\n}</script><body><input type=\\"button\\" value=\\"Print\\" onClick=\\"createWebPrintJob()\\" />\\n</body></html>"; //Loading the above transformed CSLT in to Webview... webview.loadData(html + html_button,"text/html",null); } /* * Transform XSLT to HTML string */ public static String StaticTransform(String strXsl, String strXml) { String html = ""; try { InputStream ds = null; ds = new ByteArrayInputStream(strXml.getBytes("UTF-8")); Source xmlSource = new StreamSource(ds); InputStream xs = new ByteArrayInputStream(strXsl.getBytes("UTF-8")); Source xsltSource = new StreamSource(xs); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(xsltSource); transformer.transform(xmlSource, result); html = writer.toString(); ds.close(); xs.close(); xmlSource = null; xsltSource = null; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return html; } /* * Read file from res/raw... */ private String GetStyleSheet(int fileId) { String strXsl = null; InputStream raw = getResources().openRawResource(fileId); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int size = 0; // Read the entire resource into a local byte buffer. byte[] buffer = new byte[1024]; try { while ((size = raw.read(buffer, 0, 1024)) >= 0) { outputStream.write(buffer, 0, size); } raw.close(); strXsl = outputStream.toString(); Log.v("Log", "xsl ==> " + strXsl); } catch (IOException e) { e.printStackTrace(); } return strXsl; }

Well I managed to find a solution using FileUtils to convert my file to a string:

 File InstanceXmlFile = Collect.getInstance().getFormController().getInstanceFile(); String strXML = null; try { strXML = FileUtils.readFileToString(InstanceXmlFile, "utf-8"); } catch (IOException e) { e.printStackTrace(); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM