List of usage examples for android.content.res AssetManager open
public @NonNull InputStream open(@NonNull String fileName, int accessMode) throws IOException
From source file:Main.java
public static String readFromAsstes(Context context, String fileName) { AssetManager assetManager = context.getAssets(); ByteArrayOutputStream bos = null; InputStream inputStream = null; String result = null;/*w ww . ja v a 2 s . c o m*/ try { inputStream = assetManager.open(fileName, AssetManager.ACCESS_STREAMING); bos = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int length; while ((length = inputStream.read(data)) != -1) { bos.write(data, 0, length); } result = bos.toString().replaceAll("\\s*", ""); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (bos != null) bos.close(); if (inputStream != null) inputStream.close(); } catch (IOException ignored) { } } return result; }
From source file:Main.java
public static void copyAssetFile(AssetManager am, String assetName, File destBaseDir, boolean overwrite) throws IOException { File destFile = new File(destBaseDir.getAbsolutePath().concat(File.separator).concat(assetName)); if (destFile.exists() && !overwrite) return;/*from w w w . j av a 2 s . c om*/ if (overwrite) { destFile.delete(); } destFile.getParentFile().mkdirs(); InputStream is = null; OutputStream os = null; try { byte[] buff = new byte[4096]; is = am.open(assetName, AssetManager.ACCESS_STREAMING); os = new FileOutputStream(destFile); while (true) { int nread = is.read(buff); if (nread <= 0) break; os.write(buff, 0, nread); } } finally { closeQuietly(is); closeQuietly(os); } }
From source file:tv.ouya.sdk.CordovaOuyaPlugin.java
public void initOuyaPlugin(final JSONArray jsonArray) { if (null == jsonArray) { JSONObject result = createError(0, "initOuyaPlugin jsonArray is null!"); sCallbackInitOuyaPlugin.error(result); return;//from w w w . j av a 2 s. c om } if (sEnableLogging) { Log.i(TAG, "initOuyaPlugin jsonArray=" + jsonArray.toString()); } sInitCompletedListener = new CancelIgnoringOuyaResponseListener<Bundle>() { @Override public void onSuccess(final Bundle info) { if (sEnableLogging) { Log.i(TAG, "sInitCompletedListener: onSuccess"); } cordova.getThreadPool().execute(new Runnable() { public void run() { PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "SUCCESS"); pluginResult.setKeepCallback(false); sCallbackInitOuyaPlugin.sendPluginResult(pluginResult); } }); } @Override public void onFailure(final int errorCode, final String errorMessage, final Bundle optionalData) { if (sEnableLogging) { Log.i(TAG, "sInitCompletedListener: onFailure errorCode=" + errorCode + " errorMessage=" + errorMessage); } cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(errorCode, errorMessage); PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, result); pluginResult.setKeepCallback(false); sCallbackInitOuyaPlugin.sendPluginResult(pluginResult); } }); } }; sRequestGamerInfoListener = new CancelIgnoringOuyaResponseListener<GamerInfo>() { @Override public void onSuccess(final GamerInfo info) { Log.i(TAG, "sRequestGamerInfoListener: onSuccess"); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = new JSONObject(); try { result.put("username", info.getUsername()); result.put("uuid", info.getUuid()); } catch (JSONException e) { result = createError(0, "Failed to create results!"); sCallbackRequestGamerInfo.error(result); return; } sCallbackRequestGamerInfo.success(result); } }); } @Override public void onFailure(final int errorCode, final String errorMessage, final Bundle optionalData) { Log.i(TAG, "sRequestGamerInfoListener: onFailure errorCode=" + errorCode + " errorMessage=" + errorMessage); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(errorCode, errorMessage); sCallbackRequestGamerInfo.error(result); } }); } }; sRequestProductsListener = new CancelIgnoringOuyaResponseListener<List<Product>>() { @Override public void onSuccess(final List<Product> products) { Log.i(TAG, "sRequestProductsListener: onSuccess"); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONArray result = new JSONArray(); try { int i = 0; for (Product product : products) { JSONObject jsonObject = new JSONObject(); jsonObject.put("description", product.getDescription()); jsonObject.put("identifier", product.getIdentifier()); jsonObject.put("name", product.getName()); jsonObject.put("localPrice", product.getLocalPrice()); result.put(i, jsonObject); ++i; } } catch (JSONException e) { JSONObject error = createError(0, "Failed to create results!"); sCallbackRequestProducts.error(error); return; } sCallbackRequestProducts.success(result); } }); } @Override public void onFailure(final int errorCode, final String errorMessage, final Bundle optionalData) { Log.i(TAG, "sRequestProductsListener: onFailure errorCode=" + errorCode + " errorMessage=" + errorMessage); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(errorCode, errorMessage); sCallbackRequestProducts.error(result); } }); } }; sRequestPurchaseListener = new OuyaResponseListener<PurchaseResult>() { @Override public void onSuccess(final PurchaseResult purchaseResult) { Log.i(TAG, "sRequestPurchaseListener: onSuccess"); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = new JSONObject(); try { result.put("productIdentifier", purchaseResult.getProductIdentifier()); } catch (JSONException e) { result = createError(0, "Failed to set productIdentifier!"); sCallbackRequestPurchase.error(result); return; } sCallbackRequestPurchase.success(result); } }); } @Override public void onFailure(final int errorCode, final String errorMessage, final Bundle optionalData) { Log.i(TAG, "sRequestPurchaseListener: onFailure errorCode=" + errorCode + " errorMessage=" + errorMessage); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(errorCode, errorMessage); sCallbackRequestPurchase.error(result); } }); } @Override public void onCancel() { Log.i(TAG, "sRequestPurchaseListener: onCancel"); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(0, "Purchase was cancelled!"); sCallbackRequestPurchase.error(result); } }); } }; sRequestReceiptsListener = new OuyaResponseListener<Collection<Receipt>>() { @Override public void onSuccess(final Collection<Receipt> receipts) { Log.i(TAG, "requestReceipts onSuccess: received " + receipts.size() + " receipts"); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONArray result = new JSONArray(); try { int i = 0; for (Receipt receipt : receipts) { JSONObject jsonObject = new JSONObject(); jsonObject.put("currency", receipt.getCurrency()); jsonObject.put("generatedDate", receipt.getGeneratedDate()); jsonObject.put("identifier", receipt.getIdentifier()); jsonObject.put("localPrice", receipt.getLocalPrice()); result.put(i, jsonObject); ++i; } } catch (JSONException e) { JSONObject error = createError(0, "Failed to create results!"); sCallbackRequestReceipts.error(error); return; } sCallbackRequestReceipts.success(result); } }); } @Override public void onFailure(final int errorCode, final String errorMessage, final Bundle optionalData) { Log.e(TAG, "requestReceipts onFailure: errorCode=" + errorCode + " errorMessage=" + errorMessage); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(errorCode, errorMessage); sCallbackRequestReceipts.error(result); } }); } @Override public void onCancel() { Log.i(TAG, "requestReceipts onCancel"); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(0, "Request Receipts was cancelled!"); sCallbackRequestReceipts.error(result); } }); } }; Runnable runnable = new Runnable() { public void run() { Bundle developerInfo = new Bundle(); try { for (int index = 0; index < jsonArray.length(); ++index) { JSONObject jsonObject = jsonArray.getJSONObject(index); String name = jsonObject.getString("key"); String value = jsonObject.getString("value"); if (null == name || null == value) { continue; } if (name.equals("tv.ouya.product_id_list")) { String[] productIds = value.split(","); if (null == productIds) { continue; } developerInfo.putStringArray("tv.ouya.product_id_list", productIds); } else { Log.i(TAG, "initOuyaPlugin name=" + name + " value=" + value); developerInfo.putString(name, value); } } } catch (JSONException e) { e.printStackTrace(); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(0, "initOuyaPlugin Failed to read JSON data!"); sCallbackInitOuyaPlugin.error(result); } }); return; } byte[] applicationKey = null; // load the application key from assets try { AssetManager assetManager = cordova.getActivity().getAssets(); InputStream inputStream = assetManager.open("key.der", AssetManager.ACCESS_BUFFER); applicationKey = new byte[inputStream.available()]; inputStream.read(applicationKey); inputStream.close(); } catch (IOException e) { e.printStackTrace(); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(0, "Failed to load signing key!"); sCallbackInitOuyaPlugin.error(result); } }); return; } if (null == applicationKey) { cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(0, "Failed to load signing key!"); sCallbackInitOuyaPlugin.error(result); } }); return; } developerInfo.putByteArray(OuyaFacade.OUYA_DEVELOPER_PUBLIC_KEY, applicationKey); try { sOuyaFacade = OuyaFacade.getInstance(); } catch (Exception e) { e.printStackTrace(); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(0, "Failed to get OUYA Facade instance!"); sCallbackInitOuyaPlugin.error(result); } }); return; } /* try { sOuyaFacade.registerInitCompletedListener(sInitCompletedListener); } catch (Exception e) { e.printStackTrace(); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(0, "Failed to register init completed listener!"); sCallbackInitOuyaPlugin.error(result); } }); return; } */ try { sOuyaFacade.init(cordova.getActivity(), developerInfo); } catch (Exception e) { e.printStackTrace(); cordova.getThreadPool().execute(new Runnable() { public void run() { JSONObject result = createError(0, "Failed to initialize the OuyaFacade!"); sCallbackInitOuyaPlugin.error(result); } }); return; } /* cordova.getThreadPool().execute(new Runnable() { public void run() { PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "WAIT"); pluginResult.setKeepCallback(true); sCallbackInitOuyaPlugin.sendPluginResult(pluginResult); } }); */ cordova.getThreadPool().execute(new Runnable() { public void run() { PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "SUCCESS"); pluginResult.setKeepCallback(false); sCallbackInitOuyaPlugin.sendPluginResult(pluginResult); } }); } }; cordova.getActivity().runOnUiThread(runnable); }