About Android assets reading files as file objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.