* 특정 URL에 있는 파일을 다운로드 받아서 storage로 저장을 하고 싶을경우

    public void onCreate(Bundle savedInstanceState) {
        ....
        try {
            String DownloadURL = "http://www.androes.com/exchange.xml";
            String FileName = "/mnt/sdcard/exchange.xml";
            InputStream inputStream = new URL(DownloadURL).openStream();
            
            File file = new File(FileName);
            OutputStream out = new FileOutputStream(file);
            saveRemoteFile(inputStream, out);
            out.close();
            Logger.d("androes", "File Write /mnt/sdcard/exchange.xml");
            
        } catch(Exception e){
            Logger.d("androes", "File Write Failed! /mnt/sdcard/exchange.xml");
            e.printStackTrace();
        }
    }

    public void saveRemoteFile(InputStream is, OutputStream os) throws IOException    
    {
        int c = 0;
        while((c = is.read()) != -1)
            os.write(c);
        os.flush();    
    }  


* AndroidManifest.xml 파일에 아래항목 추가

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

+ Recent posts