简体   繁体   English

如何从本地存储中的文件而不是 res/raw 中的硬编码文件中进行选择?

[英]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.我正在尝试编写一个应用程序,它将获取一个 XML 文件并使用 XSLT 将其转换为 HTML。

The structure of the XML files will always be the same, but the data inside will change. XML 文件的结构将始终相同,但其中的数据会发生变化。

I have managed to get it to work using an XML file hardcoded in res/raw.我已经设法使用 res/raw 中硬编码的 XML 文件让它工作。

How can I change the app so that I can choose an XML file from the local storage on the device?如何更改应用程序以便我可以从设备的本地存储中选择 XML 文件?

Here is my code:这是我的代码:

I have a MainActivity with just a single button that kicks off the Activity that is doing the conversion:我有一个 MainActivity ,只有一个按钮可以启动正在进行转换的活动:

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活动_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:好吧,我设法找到了使用 FileUtils 将我的文件转换为字符串的解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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