@Override public FileInputStream openFileInput(String name) throws FileNotFoundException { return mBase.openFileInput(name); }
public Bitmap getImageBitmap(Context context,String name,String extension){ name=name+"."+extension; try{ FileInputStream fis = context.openFileInput(name); Bitmap b = BitmapFactory.decodeStream(fis); fis.close(); return b; } catch(Exception e){ } return null; }
private String readFromFile(Context context) { String ret = ""; try { InputStream inputStream = context.openFileInput("config.txt"); if ( inputStream != null ) { InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String receiveString = ""; StringBuilder stringBuilder = new StringBuilder(); while ( (receiveString = bufferedReader.readLine()) != null ) { stringBuilder.append(receiveString); } inputStream.close(); ret = stringBuilder.toString(); } } catch (FileNotFoundException e) { Log.e("login activity", "File not found: " + e.toString()); } catch (IOException e) { Log.e("login activity", "Can not read file: " + e.toString()); } return ret; }
public ResponseBodyData readFile(String requestId) throws IOException { InputStream in = mContext.openFileInput(getFilename(requestId)); try { int firstByte = in.read(); if (firstByte == -1) { throw new EOFException("Failed to read base64Encode byte"); } ResponseBodyData bodyData = new ResponseBodyData(); bodyData.base64Encoded = firstByte != 0; AsyncPrettyPrinter asyncPrettyPrinter = mRequestIdMap.get(requestId); if (asyncPrettyPrinter != null) { // TODO: this line blocks for up to 10 seconds and create problems as described // in issue #243 allow asynchronous dispatch for MethodDispatcher bodyData.data = prettyPrintContentWithTimeOut(asyncPrettyPrinter, in); } else { bodyData.data = Util.readAsUTF8(in); } return bodyData; } finally { in.close(); } }
public ArrayList<ToDoItem> loadFromFile() throws IOException, JSONException { ArrayList<ToDoItem> items = new ArrayList<>(); BufferedReader bufferedReader = null; FileInputStream fileInputStream = null; try { fileInputStream = mContext.openFileInput(mFileName); StringBuilder builder = new StringBuilder(); String line; bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); while ((line = bufferedReader.readLine()) != null) { builder.append(line); } JSONArray jsonArray = (JSONArray) new JSONTokener(builder.toString()).nextValue(); for (int i = 0; i < jsonArray.length(); i++) { ToDoItem item = new ToDoItem(jsonArray.getJSONObject(i)); items.add(item); } } catch (FileNotFoundException fnfe) { //do nothing about it //file won't exist first time app is run } finally { if (bufferedReader != null) { bufferedReader.close(); } if (fileInputStream != null) { fileInputStream.close(); } } return items; }
@Test(expected = IllegalArgumentException.class) public void openFileInput_shouldNotAcceptPathsWithSeparatorCharacters() throws Exception { try (FileInputStream fileInputStream = context.openFileInput("data" + File.separator + "test")) {} }
try { ois = new ObjectInputStream( context.openFileInput(PERSISTED_SESSION_INFO_FILENAME)); appSessionInfoMap = (HashMap<AccessTokenAppIdPair, FacebookTimeSpentData>) ois.readObject();
inputStream = context.openFileInput(DISTINCT_BLUETOOTH_ADDRESSES_FILE); reader = new BufferedReader(new InputStreamReader(inputStream)); String line;
@Test public void openFileInput_shouldReturnAFileInputStream() throws Exception { String fileContents = "blah"; File file = new File(context.getFilesDir(), "__test__"); try (Writer fileWriter = Files.newBufferedWriter(file.toPath(), UTF_8)) { fileWriter.write(fileContents); } try (FileInputStream fileInputStream = context.openFileInput("__test__")) { byte[] bytes = new byte[fileContents.length()]; fileInputStream.read(bytes); assertThat(bytes).isEqualTo(fileContents.getBytes(UTF_8)); } }
ObjectInputStream objectInputStream = null; try { inputStream = mContext.openFileInput(STATUS_PRESERVATION_FILE_NAME); objectInputStream = new ObjectInputStream(inputStream); Map<Region, RegionMonitoringState> obj = (Map<Region, RegionMonitoringState>) objectInputStream.readObject();
Context context = FacebookSdk.getApplicationContext(); try { InputStream is = context.openFileInput(PERSISTED_EVENTS_FILENAME); ois = new MovedClassObjectInputStream(new BufferedInputStream(is));
public static LimiterData load(@NonNull Context context) { try { return new LimiterData(new StreamReader(context.openFileInput(FILE_LIMITER_DATA)).read()); } catch (FileNotFoundException e){ return new LimiterData(); }catch (IOException | JSONException e) { ACRA.log.w(LOG_TAG, "Failed to load LimiterData", e); return new LimiterData(); } }
ObjectInputStream objectInputStream = null; try { inputStream = context.openFileInput(STATUS_PRESERVATION_FILE_NAME); objectInputStream = new ObjectInputStream(inputStream); scanState = (ScanState) objectInputStream.readObject();
@Override public FileInputStream openFileInput(String name) throws FileNotFoundException { return mBase.openFileInput(name); }
/** * Try open the file with {@link Context#openFileInput(String)}, falling back to a file of * the same name in the assets. */ public static InputStream openRead(Context context, String filename) throws IOException { try { return context.openFileInput(filename); } catch (FileNotFoundException e) { return context.getAssets().open(filename); } }
public static Bitmap loadBitmap(Context context, String picName){ Bitmap b = null; FileInputStream fis; try { fis = context.openFileInput(picName); b = BitmapFactory.decodeStream(fis); fis.close(); } catch (FileNotFoundException e) { Log.d(TAG, "file not found"); e.printStackTrace(); } catch (IOException e) { Log.d(TAG, "io exception"); e.printStackTrace(); } return b; }
private String readCrashReportFromFile() { try { FileInputStream fis = appCtx.openFileInput(CRASHREPORT); return StreamUtils.readToString(fis); } catch (IOException ignore) {} return null; }
private String readFromFile(Context context, String str) { String str2 = null; if (context == null || str == null) { XLLog.e(XLUtil.TAG, "readFromFile, parameter invalid, fileName:" + str); } else { try { InputStream openFileInput = context.openFileInput(str); byte[] bArr = new byte[256]; try { int read = openFileInput.read(bArr); if (read > 0) { str2 = new String(bArr, 0, read, "utf-8"); } openFileInput.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e2) { XLLog.i(XLUtil.TAG, str + " File Not Found"); } } return str2; } }
@Test public void testOpenRead_fallbackToAsset() throws Exception { FileInputStream stream = mock(FileInputStream.class); when(mockContext.openFileInput(anyString())).thenThrow(new FileNotFoundException("Test")); when(mockAssets.open(anyString())).thenReturn(stream); assertSame(stream, FileHelper.openRead(mockContext, "file")); }
@Test public void testOpenRead_existingFile() throws Exception { FileInputStream stream = mock(FileInputStream.class); when(mockContext.openFileInput(anyString())).thenReturn(stream); when(mockAssets.open(anyString())).thenThrow(new IOException()); assertSame(stream, FileHelper.openRead(mockContext, "file")); }