반응형

오늘은 간단하게 사용 가능한 파일이동 함수를 알아보겠음... 

 

A 위치에서 B 위치고 파일을 이동시키고 싶을 때 미디어파일이나 기타등등 파일이동을 시키고 싶은 때 간단하게 사용가능한 함수를 공유합니다

반응형

Function : MOVE_FILE

    /**
     * @param context : Context
     * @param inputPath : 원본 파일 Path 
     * @param inputFile : 원본 파일 이름
     * @param outputPath : 옮길 폴더 경로
     */
    public static void MOVE_FILE(Context context, String inputPath, String inputFile, String outputPath){
        InputStream in = null;
        OutputStream out = null;
        try{
            File dir = new File(outputPath);
            Log.e("dir", dir.getPath());
            if(!dir.exists()){
                dir.mkdirs();
            }

            Log.e("MOVE_FILE", outputPath + "/" + inputFile + "______" + dir.getPath());

            in = new FileInputStream(inputPath + inputFile);
            out = new FileOutputStream(outputPath + "/" + inputFile);

            byte[] buffer = new byte[1024];
            int read;
            while((read = in.read(buffer)) != -1){
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;

            out.flush();
            out.close();
            out = null;

            // 기존 원본파일 삭제
            new File(inputPath + inputFile).delete();
            
            // 파일 미디어 동기화 , 사진 혹은 동영상 파일 갤러리 동기화
            File tmp_file = new File(outputPath + "/" + inputFile);
            context.sendBroadcast(new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(tmp_file)) );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
SMALL

아시는 분은 아시겠지만 사용법은 간단합니다.

 

파라미터 설명
inputPath  원본파일 Path
inputFile  원본파일 이름
outputPath  이동할 폴더 경로

 

해당 파라미터에 대한 값을 입력 후 동작시키면 원본파일은 이동할 폴더에 원본 파일명으로 생성된 뒤 원본파일은 삭제처리 됨... ㅎㅎ 

 

 

반응형

+ Recent posts