Example usage for android.content.res AssetFileDescriptor createInputStream

List of usage examples for android.content.res AssetFileDescriptor createInputStream

Introduction

In this page you can find the example usage for android.content.res AssetFileDescriptor createInputStream.

Prototype

public FileInputStream createInputStream() throws IOException 

Source Link

Document

Create and return a new auto-close input stream for this asset.

Usage

From source file:android_network.hetnet.vpn_service.ActivitySettings.java

private void handleImport(final Intent data) {
    new AsyncTask<Object, Object, Throwable>() {
        @Override/*from  w  w  w. j a v  a2 s . co  m*/
        protected Throwable doInBackground(Object... objects) {
            InputStream in = null;
            try {
                Log.i(TAG, "Reading URI=" + data.getData());
                ContentResolver resolver = getContentResolver();
                String[] streamTypes = resolver.getStreamTypes(data.getData(), "*/*");
                String streamType = (streamTypes == null || streamTypes.length == 0 ? "*/*" : streamTypes[0]);
                AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(data.getData(),
                        streamType, null);
                in = descriptor.createInputStream();
                xmlImport(in);
                return null;
            } catch (Throwable ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                return ex;
            } finally {
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
            }
        }

        @Override
        protected void onPostExecute(Throwable ex) {
            if (running) {
                if (ex == null) {
                    Toast.makeText(ActivitySettings.this, R.string.msg_completed, Toast.LENGTH_LONG).show();
                    ServiceSinkhole.reloadStats("import", ActivitySettings.this);
                    // Update theme, request permissions
                    recreate();
                } else
                    Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }.execute();
}

From source file:android_network.hetnet.vpn_service.ActivitySettings.java

private void handleHosts(final Intent data) {
    new AsyncTask<Object, Object, Throwable>() {
        @Override/*from  w  w w.j ava  2 s . c  om*/
        protected Throwable doInBackground(Object... objects) {
            File hosts = new File(getFilesDir(), "hosts.txt");

            FileOutputStream out = null;
            InputStream in = null;
            try {
                Log.i(TAG, "Reading URI=" + data.getData());
                ContentResolver resolver = getContentResolver();
                String[] streamTypes = resolver.getStreamTypes(data.getData(), "*/*");
                String streamType = (streamTypes == null || streamTypes.length == 0 ? "*/*" : streamTypes[0]);
                AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(data.getData(),
                        streamType, null);
                in = descriptor.createInputStream();
                out = new FileOutputStream(hosts);

                int len;
                long total = 0;
                byte[] buf = new byte[4096];
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                    total += len;
                }
                Log.i(TAG, "Copied bytes=" + total);

                return null;
            } catch (Throwable ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                return ex;
            } finally {
                if (out != null)
                    try {
                        out.close();
                    } catch (IOException ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ex) {
                        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                    }
            }
        }

        @Override
        protected void onPostExecute(Throwable ex) {
            if (running) {
                if (ex == null) {
                    SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(ActivitySettings.this);
                    String last = SimpleDateFormat.getDateTimeInstance().format(new Date().getTime());
                    prefs.edit().putString("hosts_last_import", last).apply();

                    if (running) {
                        getPreferenceScreen().findPreference("hosts_import")
                                .setSummary(getString(R.string.msg_import_last, last));
                        Toast.makeText(ActivitySettings.this, R.string.msg_completed, Toast.LENGTH_LONG).show();
                    }

                    ServiceSinkhole.reload("hosts import", ActivitySettings.this);
                } else
                    Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }.execute();
}