Read from Assets folder in android

Frequently used code samples and tutorials
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Read from Assets folder in android

Postby vivekkalady » Fri Nov 21, 2014 2:57 pm

Read String from Asset Folder


Code: Select all

public static String getTextFromAsset(Context ctx,String file_name) {

    AssetManager assetManager = ctx.getAssets();
    ByteArrayOutputStream outputStream = null;
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open(file_name);
        outputStream = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
        }
    } catch (IOException e) {
    }
    return outputStream.toString();

}



Load html from Assets Folder

Code: Select all

public class ViewWeb extends Activity { 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webview); 
            WebView wv; 
            wv = (WebView) findViewById(R.id.webView1); 
            wv.loadUrl("file:///android_asset/mywebpage.html");   // now it will not fail here
        } 
    }

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 1 guest

cron