Example usage for android.content.res AssetManager open

List of usage examples for android.content.res AssetManager open

Introduction

In this page you can find the example usage for android.content.res AssetManager open.

Prototype

public @NonNull InputStream open(@NonNull String fileName) throws IOException 

Source Link

Document

Open an asset using ACCESS_STREAMING mode.

Usage

From source file:org.deviceconnect.android.profile.restful.test.NormalNotificationProfileTestCase.java

/**
 * ??????????.//from  www  .  ja va2  s .c o  m
 * 
 * <pre>
 * ?HTTP
 * Method: POST
 * Path: notification/notify?deviceId=xxxx&type=0&dir=auto&tag=tag1,tag2,tag3
 * </pre>
 * 
 * <pre>
 * ??
 * result?0???????
 * notificationid?1???????
 * </pre>
 */
public void testPostNotifyOptional025() {
    StringBuilder builder = new StringBuilder();
    builder.append(DCONNECT_MANAGER_URI);
    builder.append("/" + NotificationProfileConstants.PROFILE_NAME);
    builder.append("/" + NotificationProfileConstants.ATTRIBUTE_NOTIFY);
    builder.append("?");
    builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TYPE + "=0");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_DIR + "=auto");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TAG + "=tag1,tag2,tag3");

    builder.append("&");
    builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
    try {
        HttpPost request = new HttpPost(builder.toString());
        String name = "test.png";
        AssetManager manager = getApplicationContext().getAssets();
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(NotificationProfileConstants.PARAM_ICON, new InputStreamBody(manager.open(name), name));
        request.setEntity(entity);

        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        Assert.assertEquals("notificationid is not equals.",
                TestNotificationProfileConstants.NOTIFICATION_ID[0],
                root.getString(NotificationProfileConstants.PARAM_NOTIFICATION_ID));
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    } catch (IOException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:org.deviceconnect.android.profile.restful.test.NormalNotificationProfileTestCase.java

/**
 * ??????????.//  w  w  w .  j  a v  a 2 s  .com
 * 
 * <pre>
 * ?HTTP
 * Method: POST
 * Path: notification/notify?deviceId=xxxx&type=0&lang=jp-JP&tag=tag1,tag2,tag3
 * </pre>
 * 
 * <pre>
 * ??
 * result?0???????
 * notificationid?1???????
 * </pre>
 */
public void testPostNotifyOptional026() {
    StringBuilder builder = new StringBuilder();
    builder.append(DCONNECT_MANAGER_URI);
    builder.append("/" + NotificationProfileConstants.PROFILE_NAME);
    builder.append("/" + NotificationProfileConstants.ATTRIBUTE_NOTIFY);
    builder.append("?");
    builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TYPE + "=0");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_LANG + "=jp-JP");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TAG + "=tag1,tag2,tag3");

    builder.append("&");
    builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
    try {
        HttpPost request = new HttpPost(builder.toString());
        String name = "test.png";
        AssetManager manager = getApplicationContext().getAssets();
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(NotificationProfileConstants.PARAM_ICON, new InputStreamBody(manager.open(name), name));
        request.setEntity(entity);

        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        Assert.assertEquals("notificationid is not equals.",
                TestNotificationProfileConstants.NOTIFICATION_ID[0],
                root.getString(NotificationProfileConstants.PARAM_NOTIFICATION_ID));
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    } catch (IOException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:org.deviceconnect.android.profile.restful.test.NormalNotificationProfileTestCase.java

/**
 * ??????????.//  w ww . j  a v  a 2  s .  c o  m
 * 
 * <pre>
 * ?HTTP
 * Method: POST
 * Path: notification/notify?deviceId=xxxx&type=0&body=test_body&tag=tag1,tag2,tag3
 * </pre>
 * 
 * <pre>
 * ??
 * result?0???????
 * notificationid?1???????
 * </pre>
 */
public void testPostNotifyOptional028() {
    StringBuilder builder = new StringBuilder();
    builder.append(DCONNECT_MANAGER_URI);
    builder.append("/" + NotificationProfileConstants.PROFILE_NAME);
    builder.append("/" + NotificationProfileConstants.ATTRIBUTE_NOTIFY);
    builder.append("?");
    builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TYPE + "=0");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_BODY + "=test_body");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TAG + "=tag1,tag2,tag3");

    builder.append("&");
    builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
    try {
        HttpPost request = new HttpPost(builder.toString());
        String name = "test.png";
        AssetManager manager = getApplicationContext().getAssets();
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(NotificationProfileConstants.PARAM_ICON, new InputStreamBody(manager.open(name), name));
        request.setEntity(entity);

        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        Assert.assertEquals("notificationid is not equals.",
                TestNotificationProfileConstants.NOTIFICATION_ID[0],
                root.getString(NotificationProfileConstants.PARAM_NOTIFICATION_ID));
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    } catch (IOException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:org.deviceconnect.android.profile.restful.test.NormalNotificationProfileTestCase.java

/**
 * ??????????./*from ww w  . ja va  2  s.c  o m*/
 * 
 * <pre>
 * ?HTTP
 * Method: POST
 * Path: notification/notify?deviceId=xxxx&type=0&dir=auto&lang=jp-JP&body=test_body
 * </pre>
 * 
 * <pre>
 * ??
 * result?0???????
 * notificationid?1???????
 * </pre>
 */
public void testPostNotifyOptional023() {
    StringBuilder builder = new StringBuilder();
    builder.append(DCONNECT_MANAGER_URI);
    builder.append("/" + NotificationProfileConstants.PROFILE_NAME);
    builder.append("/" + NotificationProfileConstants.ATTRIBUTE_NOTIFY);
    builder.append("?");
    builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TYPE + "=0");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_DIR + "=auto");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_LANG + "=jp-JP");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_BODY + "=test_body");

    builder.append("&");
    builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
    try {
        HttpPost request = new HttpPost(builder.toString());
        String name = "test.png";
        AssetManager manager = getApplicationContext().getAssets();
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(NotificationProfileConstants.PARAM_ICON, new InputStreamBody(manager.open(name), name));
        request.setEntity(entity);

        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        Assert.assertEquals("notificationid is not equals.",
                TestNotificationProfileConstants.NOTIFICATION_ID[0],
                root.getString(NotificationProfileConstants.PARAM_NOTIFICATION_ID));
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    } catch (IOException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:org.deviceconnect.android.profile.restful.test.NormalNotificationProfileTestCase.java

/**
 * ??????????.//  w  w w.ja v  a  2s .co m
 * 
 * <pre>
 * ?HTTP
 * Method: POST
 * Path: notification/notify?deviceId=xxxx&type=0&dir=auto&lang=jp-JP&tag=tag1,tag2,tag3
 * </pre>
 * 
 * <pre>
 * ??
 * result?0???????
 * notificationid?1???????
 * </pre>
 */
public void testPostNotifyOptional027() {
    StringBuilder builder = new StringBuilder();
    builder.append(DCONNECT_MANAGER_URI);
    builder.append("/" + NotificationProfileConstants.PROFILE_NAME);
    builder.append("/" + NotificationProfileConstants.ATTRIBUTE_NOTIFY);
    builder.append("?");
    builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TYPE + "=0");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_DIR + "=auto");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_LANG + "=jp-JP");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TAG + "=tag1,tag2,tag3");

    builder.append("&");
    builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
    try {
        HttpPost request = new HttpPost(builder.toString());
        String name = "test.png";
        AssetManager manager = getApplicationContext().getAssets();
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(NotificationProfileConstants.PARAM_ICON, new InputStreamBody(manager.open(name), name));
        request.setEntity(entity);

        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        Assert.assertEquals("notificationid is not equals.",
                TestNotificationProfileConstants.NOTIFICATION_ID[0],
                root.getString(NotificationProfileConstants.PARAM_NOTIFICATION_ID));
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    } catch (IOException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:org.deviceconnect.android.profile.restful.test.NormalNotificationProfileTestCase.java

/**
 * ??????????./* ww w . j  a  v  a  2 s  .c  o  m*/
 * 
 * <pre>
 * ?HTTP
 * Method: POST
 * Path: notification/notify?deviceId=xxxx&type=0&dir=auto&body=test_body&tag=tag1,tag2,tag3
 * </pre>
 * 
 * <pre>
 * ??
 * result?0???????
 * notificationid?1???????
 * </pre>
 */
public void testPostNotifyOptional029() {
    StringBuilder builder = new StringBuilder();
    builder.append(DCONNECT_MANAGER_URI);
    builder.append("/" + NotificationProfileConstants.PROFILE_NAME);
    builder.append("/" + NotificationProfileConstants.ATTRIBUTE_NOTIFY);
    builder.append("?");
    builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TYPE + "=0");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_DIR + "=auto");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_BODY + "=test_body");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TAG + "=tag1,tag2,tag3");

    builder.append("&");
    builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
    try {
        HttpPost request = new HttpPost(builder.toString());
        String name = "test.png";
        AssetManager manager = getApplicationContext().getAssets();
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(NotificationProfileConstants.PARAM_ICON, new InputStreamBody(manager.open(name), name));
        request.setEntity(entity);

        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        Assert.assertEquals("notificationid is not equals.",
                TestNotificationProfileConstants.NOTIFICATION_ID[0],
                root.getString(NotificationProfileConstants.PARAM_NOTIFICATION_ID));
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    } catch (IOException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:org.deviceconnect.android.profile.restful.test.NormalNotificationProfileTestCase.java

/**
 * ??????????./*from w  w  w.  ja  va  2s. c o m*/
 * 
 * <pre>
 * ?HTTP
 * Method: POST
 * Path: notification/notify?deviceId=xxxx&type=0&lang=jp-JP&body=test_body&tag=tag1,tag2,tag3
 * </pre>
 * 
 * <pre>
 * ??
 * result?0???????
 * notificationid?1???????
 * </pre>
 */
public void testPostNotifyOptional030() {
    StringBuilder builder = new StringBuilder();
    builder.append(DCONNECT_MANAGER_URI);
    builder.append("/" + NotificationProfileConstants.PROFILE_NAME);
    builder.append("/" + NotificationProfileConstants.ATTRIBUTE_NOTIFY);
    builder.append("?");
    builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TYPE + "=0");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_LANG + "=jp-JP");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_BODY + "=test_body");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TAG + "=tag1,tag2,tag3");

    builder.append("&");
    builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
    try {
        HttpPost request = new HttpPost(builder.toString());
        String name = "test.png";
        AssetManager manager = getApplicationContext().getAssets();
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(NotificationProfileConstants.PARAM_ICON, new InputStreamBody(manager.open(name), name));
        request.setEntity(entity);

        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        Assert.assertEquals("notificationid is not equals.",
                TestNotificationProfileConstants.NOTIFICATION_ID[0],
                root.getString(NotificationProfileConstants.PARAM_NOTIFICATION_ID));
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    } catch (IOException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:org.deviceconnect.android.profile.restful.test.NormalNotificationProfileTestCase.java

/**
 * ??????????.//from  www.  j a  v a  2  s .  c  om
 * 
 * <pre>
 * ?HTTP
 * Method: POST
 * Path: notification/notify?deviceId=xxxx&type=0&dir=auto&lang=jp-JP&body=test_body&tag=tag1,tag2,tag3
 * </pre>
 * 
 * <pre>
 * ??
 * result?0???????
 * notificationid?1???????
 * </pre>
 */
public void testPostNotifyOptional031() {
    StringBuilder builder = new StringBuilder();
    builder.append(DCONNECT_MANAGER_URI);
    builder.append("/" + NotificationProfileConstants.PROFILE_NAME);
    builder.append("/" + NotificationProfileConstants.ATTRIBUTE_NOTIFY);
    builder.append("?");
    builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TYPE + "=0");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_DIR + "=auto");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_LANG + "=jp-JP");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_BODY + "=test_body");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TAG + "=tag1,tag2,tag3");

    builder.append("&");
    builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
    try {
        HttpPost request = new HttpPost(builder.toString());
        String name = "test.png";
        AssetManager manager = getApplicationContext().getAssets();
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(NotificationProfileConstants.PARAM_ICON, new InputStreamBody(manager.open(name), name));
        request.setEntity(entity);

        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        Assert.assertEquals("notificationid is not equals.",
                TestNotificationProfileConstants.NOTIFICATION_ID[0],
                root.getString(NotificationProfileConstants.PARAM_NOTIFICATION_ID));
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    } catch (IOException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:android.webkit.cts.WebViewTest.java

public void testRequestImageRef() throws Exception, Throwable {
    if (!NullWebViewUtils.isWebViewAvailable()) {
        return;/*from   w  w  w .  j  a v  a  2  s  .  c  om*/
    }
    final class ImageLoaded {
        public boolean mImageLoaded;

        @JavascriptInterface
        public void loaded() {
            mImageLoaded = true;
        }
    }
    final ImageLoaded imageLoaded = new ImageLoaded();
    runTestOnUiThread(new Runnable() {
        public void run() {
            mOnUiThread.getSettings().setJavaScriptEnabled(true);
        }
    });
    mOnUiThread.addJavascriptInterface(imageLoaded, "imageLoaded");
    AssetManager assets = getActivity().getAssets();
    Bitmap bitmap = BitmapFactory.decodeStream(assets.open(TestHtmlConstants.LARGE_IMG_URL));
    int imgWidth = bitmap.getWidth();
    int imgHeight = bitmap.getHeight();

    startWebServer(false);
    final String imgUrl = mWebServer.getAssetUrl(TestHtmlConstants.LARGE_IMG_URL);
    mOnUiThread.loadDataAndWaitForCompletion(
            "<html><head><title>Title</title><style type=\"text/css\">"
                    + "#imgElement { -webkit-transform: translate3d(0,0,1); }"
                    + "#imgElement.finish { -webkit-transform: translate3d(0,0,0);"
                    + " -webkit-transition-duration: 1ms; }</style>"
                    + "<script type=\"text/javascript\">function imgLoad() {"
                    + "imgElement = document.getElementById('imgElement');"
                    + "imgElement.addEventListener('webkitTransitionEnd',"
                    + "function(e) { imageLoaded.loaded(); });" + "imgElement.className = 'finish';}</script>"
                    + "</head><body><img id=\"imgElement\" src=\"" + imgUrl + "\" width=\"" + imgWidth
                    + "\" height=\"" + imgHeight + "\" onLoad=\"imgLoad()\"/></body></html>",
            "text/html", null);
    new PollingCheck() {
        @Override
        protected boolean check() {
            return imageLoaded.mImageLoaded;
        }
    }.run();
    getInstrumentation().waitForIdleSync();

    final HrefCheckHandler handler = new HrefCheckHandler(mWebView.getHandler().getLooper());
    final Message msg = new Message();
    msg.setTarget(handler);

    // touch the image
    handler.reset();
    int[] location = mOnUiThread.getLocationOnScreen();

    long time = SystemClock.uptimeMillis();
    getInstrumentation().sendPointerSync(MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN,
            location[0] + imgWidth / 2, location[1] + imgHeight / 2, 0));
    getInstrumentation().waitForIdleSync();
    mOnUiThread.requestImageRef(msg);
    new PollingCheck() {
        @Override
        protected boolean check() {
            boolean done = false;
            if (handler.hasCalledHandleMessage()) {
                if (handler.mResultUrl != null) {
                    done = true;
                } else {
                    handler.reset();
                    Message newMsg = new Message();
                    newMsg.setTarget(handler);
                    mOnUiThread.requestImageRef(newMsg);
                }
            }
            return done;
        }
    }.run();
    assertEquals(imgUrl, handler.mResultUrl);
}

From source file:com.androzic.Androzic.java

/**
 * Copies file assets from installation package to filesystem.
 *///w  w  w.  j av  a 2  s.  c o m
public void copyAssets(String folder, File path) {
    Log.i(TAG, "CopyAssets(" + folder + ", " + path + ")");
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list(folder);
    } catch (IOException e) {
        Log.e("Androzic", "Failed to get assets list", e);
        return;
    }
    for (String file : files) {
        try {
            InputStream in = assetManager.open(folder + "/" + file);
            OutputStream out = new FileOutputStream(new File(path, file));
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            out.flush();
            out.close();
        } catch (Exception e) {
            Log.e("Androzic", "Asset copy error", e);
        }
    }
}