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

AsyncTask 프로그레스바 출력을 하는데 음수 값이 출력됩니다.

0 추천
private class test extends AsyncTask<String, String, String> {
        private ProgressDialog mDlg;
        @Override
        protected String doInBackground(String... params) {
            String suc = "F";
            int count = 0;
            long now = System.currentTimeMillis();
            String localPath = myPath + now + ".mp3";
            InputStream isFile = null;
            FileOutputStream fosFile = null;
 
            try {
 
                URL myUrl = new URL(pinUrl3);
                URI uri = new URI(myUrl.getProtocol(), myUrl.getUserInfo(), myUrl.getHost(), myUrl.getPort(),
                        myUrl.getPath(), myUrl.getQuery(), myUrl.getRef());
                myUrl = uri.toURL();
                // URL imgUrl = new URL(pinUrl);
                // ������ �����ϴ� Ŭ���̾�Ʈ ��ü ����
                HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
                conn.setRequestProperty("Accept-Encoding", "identity");//add
                conn.setConnectTimeout(2000);
                int fileSize = conn.getContentLength();// 다운받을 파일 크기
                double kilobytes = (fileSize / 1024);
                double megabytes = (kilobytes / 1024);
                Log.e("다운로드파일사이즈:", megabytes + "MB");
                if (fileSize < 0) {
                    conn.disconnect();
                    suc = "F";
                } else {
                    byte[] data = new byte[fileSize];
                    InputStream is = conn.getInputStream();
                    File file = new File(localPath);
                    if (!file.exists())
                    {
                        FileOutputStream fos = new FileOutputStream(file);
                        int read;
                        long total=0;
                        int progress = 0;
                        for (;;)
                        {
                            read = is.read(data);
                            total += read;
                            int progress_temp = (int) total * 100 / fileSize;
                            publishProgress(""+ progress_temp);
                            if (progress_temp % 10 == 0 && progress != progress_temp)
                            {
                                progress = progress_temp;
                            }
                            if (read <= 0)
                            {
                                break;
                            }
                            fos.write(data, 0, read);
                        }
                        is.close();
                        fos.close();
                        conn.disconnect();
                 
                        suc = "T";
                    }
                }
            } catch (ConnectTimeoutException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return suc;
        }
 
         private void publishProgress(String string)
         {
         // TODO Auto-generated method stub
         Log.e("publishProgress",Integer.parseInt(string)+"");
         mDlg.setProgress(Integer.parseInt(string));
         }

 

위와 같이 HttpURLConnection ,url을 사용해서 파일을 다운로드 받고, 

진행상황을 보여주기 위해 프로그레스바도 만들었습니다. 

용량은 최소 80mb 정도부터 시작하고요.. 

테스트 해보니 파일 다운로드는 정상적으로 작동 하는데, 프로그레스바로 넘겨주는 

값이 음수값으로 출력됩니다; 그래서 중간에 바가 다시 줄어들어버리는 현상이 발생합니다.

ok 님이 2017년 6월 29일 질문

1개의 답변

0 추천
 
채택된 답변
int progress_temp = (int) total * 100 / fileSize;

--> 아래로 바꾸어 주세요. total*100을 곱한 값이 integer max값을 넘어가면서 마이너스가 된 것 같네요.

int progress_temp = (int) ((double)total / fileSize * 100);
Will Kim (43,170 포인트) 님이 2017년 6월 29일 답변
답변 감사합니다..!
타입에 신경쓰지 않아서 그런거였군요..
수정했더니 제대로 나옵니다..감사합니다.
...