About Android Assets read files for file object problem, assets inside the file, in the use of the time, is generally used Assetmanger object, open method to get InputStream
And then do other things.
Here encountered this demand, directly to the assets inside the file read as a Files object, find a half-day, did not find such a method, search for a long time, found that this is not feasible.
Can not be obtained directly from asset and then converted directly to the file object, because asset is stored as an apk, unless you unzip the APK file, it is generally not possible to find a path to instantiate a file object,
There are also special cases where webview can load the. html file stored in asset according to the asset path:
WebView wv = new WebView(context); wv.loadUrl("file:///android_asset/help/index.html");
If you need a file, you need to copy it from scratch and store the file on the device.
And then use it again.
public static void Writebytestofile (InputStream is, file file) throws ioexception{ fileoutputstream fos = null; try { byte[] data = new byte[2048]; int nbread = 0; FOS = new FileOutputStream (file); while ((Nbread=is.read (data)) >-1) { fos.write (data,0,nbread); } } catch (Exception ex) { logger.error ("Exception", ex); } finally{ if (fos!=null) { fos.close ();}} }
or directly inputstream into string, and then perform other operations.
Assetmanager am = getactivity (). Getassets (); InputStream InputStream = Am.open ("Chapter1/observablevsiterator.java"); String json = null; try { int size = inputstream.available (); byte[] buffer = new Byte[size]; Inputstream.read (buffer); Inputstream.close (); JSON = new String (buffer, "UTF-8"); } catch (IOException ex) { ex.printstacktrace (); return null; }
About Android assets reading files as file objects