마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

프래그먼트안에서 웹페이지 오픈 한 후 파일 다운로드

0 추천
안녕하세요.

궁금한것이 있어서 글 올립니다.

프래그먼트 안에서 웹페이지를 보져주는데요.

일반 웹 앱(크롬 기타등등)에서는 링크 클릭을하면 파일이 자동 다운로드되었는데

제가 구성한 웹화면에서는 웹페이지는 보여지는데 파일이 다운이 안되네요.

 

혹시 뭐 놓친것이 있을까요?
케이간츠 (180 포인트) 님이 2015년 10월 16일 질문

1개의 답변

0 추천

1. http://stackoverflow.com/questions/17094922/how-can-i-download-the-file-by-using-webview-already-search-for-the-answer-but

여기보시면요... 

Implement a WebViewClient to use with your WebView. In it, override theshouldOverrideUrlLoading method, where you should check if it's an mp3 file, and then pass that URL to the DownloadManager or whatever you're using to actually download the file. Here's a rough idea:

잘 모르지만 직역 해 보자면요. 사용하시는 WebView에 WebViewClient 를 구현하시고 shouldOverrideUrlLoading 이 메소드 오버라이딩 하시라네요. 

그리고,

 // This will handle downloading. It requires Gingerbread, though
    final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

    // This is where downloaded files will be written, using the package name isn't required
    // but it's a good way to communicate who owns the directory
    final File destinationDir = new File (Environment.getExternalStorageDirectory(), getPackageName());
    if (!destinationDir.exists()) {
        destinationDir.mkdir(); // Don't forget to make the directory if it's not there
    }
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading (WebView view, String url) {
            boolean shouldOverride = false;
            // We only want to handle requests for mp3 files, everything else the webview
            // can handle normally
            if (url.endsWith(".mp3")) {
                shouldOverride = true;
                Uri source = Uri.parse(url);

                // Make a new request pointing to the mp3 url
                DownloadManager.Request request = new DownloadManager.Request(source);
                // Use the same file name for the destination
                File destinationFile = new File (destinationDir, source.getLastPathSegment());
                request.setDestinationUri(Uri.fromFile(destinationFile));
                // Add it to the manager
                manager.enqueue(request);
            }
            return shouldOverride;
        }
    });

 

뭐 이런식으로, 구현해 보시면 된다는거 같네요. 저도 서치만 한 것이라 실행 여부는 모르겠지만,

한번 응용해 보시면 좋을것 같습니다. 

 

2. 아니시면,

https://github.com/delight-im/Android-AdvancedWebView/blob/master/Source/src/im/delight/android/webview/AdvancedWebView.java

여기 한번 참고해 보세요...

 

사용한 구글 검색어 : webview file download in fragment android

이상입니다.

익명사용자 님이 2015년 10월 16일 답변
...