android.content.Context.openFileInput()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(243)

本文整理了Java中android.content.Context.openFileInput()方法的一些代码示例,展示了Context.openFileInput()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.openFileInput()方法的具体详情如下:
包路径:android.content.Context
类名称:Context
方法名:openFileInput

Context.openFileInput介绍

暂无

代码示例

代码示例来源:origin: oasisfeng/condom

@Override public FileInputStream openFileInput(String name) throws FileNotFoundException {
  return mBase.openFileInput(name);
}

代码示例来源:origin: stackoverflow.com

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;
}

代码示例来源:origin: stackoverflow.com

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;
}

代码示例来源:origin: facebook/stetho

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();
 }
}

代码示例来源:origin: avjinder/Minimal-Todo

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;
}

代码示例来源:origin: robolectric/robolectric

@Test(expected = IllegalArgumentException.class)
public void openFileInput_shouldNotAcceptPathsWithSeparatorCharacters() throws Exception {
 try (FileInputStream fileInputStream = context.openFileInput("data" + File.separator + "test")) {}
}

代码示例来源:origin: facebook/facebook-android-sdk

try {
  ois = new ObjectInputStream(
    context.openFileInput(PERSISTED_SESSION_INFO_FILENAME));
  appSessionInfoMap = (HashMap<AccessTokenAppIdPair, FacebookTimeSpentData>)
      ois.readObject();

代码示例来源:origin: AltBeacon/android-beacon-library

inputStream = context.openFileInput(DISTINCT_BLUETOOTH_ADDRESSES_FILE);
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;

代码示例来源:origin: robolectric/robolectric

@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));
 }
}

代码示例来源:origin: AltBeacon/android-beacon-library

ObjectInputStream objectInputStream = null;
try {
  inputStream = mContext.openFileInput(STATUS_PRESERVATION_FILE_NAME);
  objectInputStream = new ObjectInputStream(inputStream);
  Map<Region, RegionMonitoringState> obj = (Map<Region, RegionMonitoringState>) objectInputStream.readObject();

代码示例来源:origin: facebook/facebook-android-sdk

Context context = FacebookSdk.getApplicationContext();
try {
  InputStream is = context.openFileInput(PERSISTED_EVENTS_FILENAME);
  ois = new MovedClassObjectInputStream(new BufferedInputStream(is));

代码示例来源:origin: ACRA/acra

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();
  }
}

代码示例来源:origin: AltBeacon/android-beacon-library

ObjectInputStream objectInputStream = null;
try {
  inputStream = context.openFileInput(STATUS_PRESERVATION_FILE_NAME);
  objectInputStream = new ObjectInputStream(inputStream);
  scanState = (ScanState) objectInputStream.readObject();

代码示例来源:origin: Trumeet/MiPushFramework

@Override public FileInputStream openFileInput(String name) throws FileNotFoundException {
  return mBase.openFileInput(name);
}

代码示例来源:origin: julian-klode/dns66

/**
 * 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);
  }
}

代码示例来源:origin: stackoverflow.com

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; 
}

代码示例来源:origin: westnordost/StreetComplete

private String readCrashReportFromFile()
{
  try
  {
    FileInputStream fis = appCtx.openFileInput(CRASHREPORT);
    return StreamUtils.readToString(fis);
  }
  catch (IOException ignore) {}
  return null;
}

代码示例来源:origin: kingthy/TVRemoteIME

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;
  }
}

代码示例来源:origin: julian-klode/dns66

@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"));
}

代码示例来源:origin: julian-klode/dns66

@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"));
}

相关文章

Context类方法