MODE_PRIVATE

안드로이드 2015. 1. 8. 12:55
개발을 하다보면, 사용자에게 보여주지 말아야할 리소스들이 있다. 그런 경우 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


참고 : http://codeflow.co.kr/question/837/%ED%8C%8C%EC%9D%BC%EC%97%90-%EB%8D%B0%EC%9D%B4%ED%84%B0-%EC%A0%80%EC%9E%A5%ED%95%98%EA%B8%B0/

'안드로이드' 카테고리의 다른 글

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
Posted by 자바리즘
,