android db 를 external stroage에 저장

Posted by Albert 2852Day 19Hour 2Min 8Sec ago [2017-06-28]

가끔 안드로이드 개발시 db파일의 내용을 확인하고 싶을때가 있다

루팅폰이 있다면 쉽게 확인하나 아닐경우 조금 번거로와서 개발시에만 로컬 스토리지에 생성된 디비 파일을 복사는메소드를 구현해봤다.

/** * 앱의 로컬DB를, external stroage에 저장한다. * 루트/hunet_mlc/HUNET_MLC.db * @param context context */ public static void pullDbToSd(Context context) { try{ File file = new File(context.getDatabasePath("HUNET_MLC.db").getCanonicalPath()); FileInputStream is = new FileInputStream(file); File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/hunet_mlc"); if(!dir.exists()) dir.mkdir(); File outputFile = new File(dir, "HUNET_MLC.db"); if(outputFile.exists()) outputFile.delete(); outputFile.createNewFile(); FileOutputStream fous = new FileOutputStream(outputFile); byte[] buffer = new byte[(int)file.length()]; is.read(buffer); fous.write(buffer); is.close(); fous.close(); }catch(Exception e){ e.printStackTrace(); } }




LIST

Copyright © 2014 visionboy.me All Right Reserved.