홈>
몇 초가 걸리는 dropbox에서 파일을 다운로드하고 있습니다.
ProgressDialog
를 추가하고 싶습니다
다운로드 용이지만 그 방법을 모르겠습니다.
public class DownloadFile extends AsyncTask<Void, Long, Boolean> {
DownloadFile(Context context ,DropboxAPI<?> mApi ,String dropboxpath,String sdpath,int pos,int s,ArrayList<String> folder) throws DropboxException {
FileOutputStream mFos;
File file=new File(sdpath);
String path = dropboxpath;
try{
mFos = new FileOutputStream(file);
mApi.getFile(path, null, mFos, null);
}catch (Exception e) {
// TODO: handle exception
}
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
- 답변 # 1
- 답변 # 2
이 간단한 코드 @sachin 사용
public class DownloadFile extends AsyncTask<Void, Void, Void> { Home home; ProgressDialog dialog = null; public DownloadFile(Home home) { // TODO Auto-generated constructor stub this.home = home; } @Override protected Void doInBackground(Void... params) { // TODO Auto-generated method stub //Call hare method for download return null; } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub super.onPostExecute(result); dialog.dismiss(); } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); dialog = ProgressDialog.show(home, "Downloading......", "", true); } }
- 답변 # 3
이 기사는 다음에 유용합니다.
http://huuah.com/android-progress-bar-and-thread-updating/
스레드의 run () 메소드 안에 다음과 같은 함수를 호출 할 수 있습니다 :
public boolean download(String url, String path, String fileName, Handler progressHandler) { try { URL sourceUrl = new URL(formatUrl(url)); if (fileName == null || fileName.length() <= 0) { fileName = sourceUrl.getFile(); } if (fileName == null || fileName.length() <= 0) { throw new Exception("EMPTY_FILENAME_NOT_ALLOWED"); } File targetPath = new File(path); targetPath.mkdirs(); if (!targetPath.exists()) { throw new Exception("MISSING_TARGET_PATH"); } File file = new File(targetPath, fileName); URLConnection ucon = sourceUrl.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(100); int current = 0; int totalSize = ucon.getContentLength(); while ((current = bis.read()) != -1) { baf.append((byte) current); // BEGIN - Handler feedback if (progressHandler != null && (baf.length() % 100) == 0) { Message msg = progressHandler.obtainMessage(); Bundle b = new Bundle(); if (totalSize > 0) { b.putInt("total", totalSize); b.putInt("step", baf.length()); b.putBoolean("working", true); } msg.setData(b); progressHandler.handleMessage(msg); } // END } FileOutputStream fos = new FileOutputStream(file); fos.write(baf.toByteArray()); fos.close(); // BEGIN - Handler feedback if (progressHandler != null) { Message msg = progressHandler.obtainMessage(); Bundle b = new Bundle(); if (totalSize > 0) { b.putInt("total", 0); b.putInt("step", 0); b.putBoolean("working", false); } msg.setData(b); progressHandler.handleMessage(msg); } // END return file.exists(); }
이렇게하면 다운로드의 실제 진행 상황 (바이트 당 바이트)에 대한보다 정확한 피드백이 있습니다.
- 답변 # 4
AsyncTask에는 실제로 4 가지 방법이 있습니다 :
<올>onPreExecute()
-여기에서 사전 실행 작업을 수행 할 수 있습니다.doInBackground()
-여기에서 백그라운드 작업을 수행 할 수 있습니다.onPostExecute()
-여기서 사후 실행 작업을 수행 할 수 있습니다. ListView에 데이터 표시, TextView 업데이트 등과 같은 수단onProgressUpdate()
-백그라운드 작업이 진행되는 동안 UI를 업데이트하려면귀하의 경우
onPreExecute()
내부에 진행률 대화 상자 또는 진행률 표시 줄을 표시 할 수 있습니다 AsyncTask의 메소드와onPostExecute()
에서 동일하게 dismiss (()) .
관련 자료
- javascript - discordjs v12 - 역할을 추가하는 방법은 무엇입니까?
- javascript - Vuejs에 Jitsi Meet를 추가하는 방법
- javascript - html 웹 사이트에 웹 사이트 링크를 추가하는 방법은 무엇입니까? - html, css, js
- css : 클립 경로에 경로를 추가하는 방법
- xaml - 동일한 창 WPF에 두 개의 탭 컨트롤을 추가하는 방법은 무엇입니까?
- javascript - 약속에 시간 제한을 추가하는 방법은 무엇입니까?
- python - 목록의 모든 항목에 번호를 추가하는 방법은 무엇입니까?
- sql - 여러 "태그"에 대한 참조를 추가하는 방법은 무엇입니까?
- javascript - Nodejs를 사용하는 URL에 웹앱 이름을 어떻게 추가 할 수 있습니까?
- google chrome - 내 URL에 JavaScript를 매개 변수로 추가하려면 어떻게해야합니까?
- HTML에 여러 인용문을 추가하는 방법은 무엇입니까?
- javascript - useState 배열을 useState 목록으로 추가하는 방법
- mysql - 한 테이블의 데이터를 두 번째 테이블에 추가하지만 일치하는 행에만 데이터를 추가하는 방법
- php - 조명기에 JSON을 추가하는 방법
- mongodb - 다른 필드에서 필드를 추가하는 방법
- python - 출력에 텍스트를 추가하는 방법
- python - notepad ++로 __init__ 함수 끝에 "-> none - "을 추가하는 방법은 무엇입니까?
- JSON에서 객체 배열을 추가하는 방법
- javascript - Object Vuejs에 새 개체를 추가하는 방법
- javascript - Vue 3 사용 및 플러그인 Boostrap-vue 추가 방법?
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- JavaScript 변수를 HTML div에 '출력'하는 방법
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- python - 화면에서 찾은 요소를 찾을 수없는 경우 셀레늄
이렇게하십시오 :