개발을 하다보면, 사용자에게 보여주지 말아야할 리소스들이 있다. 그런 경우 ExternalStorage 보단 InternalStorage에 저장하는것이 보안상 좋다.
내부 캐쉬 파일 쓰기
내부 캐쉬 파일 읽기
내부 캐쉬 파일 쓰기
String filePath = "abcde.png"; FileOutputStream outputStream = null; try { outputStream = _context.openFileOutput(filePath, Context.MODE_PRIVATE); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
위의 openFileOutput 인수중에 Context.MODE_PRIVATE 이녀석이 중요한 녀석입니다. 내부 Cache에 저장하겠다는 의미 입니다.
openFileOutput, openFileInput은 안드로이드 Context 클래스의 내장 함수 입니다.
내부 캐쉬 파일 읽기
private Context _context; //Context 는 알아서 받아오자^^ FileInputStream inputStream = null; try { String filePath = "abcde.png"; inputStream = _context.openFileInput(filePath); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); Bitmap copyBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); bitmap.recycle(); return copyBitmap; } catch (Exception e) { e.printStackTrace(); return null; } finally { IOUtils.closeQuietly(inputStream); //IOUtils 라는 라이브러리 안에 있는 함수. Stream을 해제 해준다. }
참조 : http://developer.android.com/intl/ko/training/basics/data-storage/files.html
'안드로이드' 카테고리의 다른 글
windowSoftInputMode (0) | 2015.02.10 |
---|---|
ViewPager 와 SwipeRefreshLayout (2) | 2015.01.19 |
Fragment onCreateView inflate Exception (0) | 2014.12.26 |
TextView Font Size 코드 (0) | 2014.11.06 |
InputStream > Byte Array (0) | 2014.07.17 |