开发者

android AsyncTask using HttpURLConnection lenghtOfFile is -1 need help

i am trying to download mp file in back ground using AsyncTask into the emulator it works properly.. but in divice it doesn't show the progressbar this problem is due to the when i am running this code the

 int  lenghtOfFile = c.getContentLength();
 Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

that gives me -1 why this happning? my code is

 @Override
            protected String doInBackground(String... arg0) {
                // TODO Auto-generated method stub
                // mp3load();
                int len1 = 0;
                int count;
  开发者_C百科              try {
                    URL url = new URL(GlobalVariable.Getstr());
                    HttpURLConnection c = (HttpURLConnection) url.openConnection();
                    c.setRequestMethod("GET");
                    c.setDoOutput(true);
                    c.connect();
                    int lenghtOfFile = c.getContentLength();
                    Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
                    String PATH = Environment.getExternalStorageDirectory()
                            + "/download/";
                    Log.v(LOG_TAG, "PATH: " + PATH);
                    File file = new File(PATH);
                    file.mkdirs();

                    // String fileName = "workTest.mp3";
                    String fileName = GlobalVariable.Getstrpath().toString();

                    File outputFile = new File(file, fileName);

                    FileOutputStream fos = new FileOutputStream(outputFile);

                    InputStream is = c.getInputStream();

                    byte[] buffer = new byte[1024];
                    long total = 0;

                    while ((count = is.read(buffer)) != -1) {
                        total += count;
                        publishProgress("" + (int) ((total * 100) / lenghtOfFile));
                        // fos.write(buffer, 0, len1);
                        fos.write(buffer, 0, count);
                    }
                    fos.close();
                    is.close();

                } catch (IOException e) {
                    Log.d(LOG_TAG, "Error: " + e);
                }
                return null;
            }

            protected void onProgressUpdate(String... progress) {
                Log.d("ANDRO_ASYNC", progress[0]);

                mProgressDialog.setProgress(Integer.parseInt(progress[0]));
            }

            protected void onPostExecute(String unused) {
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
                refreshList();
                Toast.makeText(
                        FindFilesByType.this,
                        "Downloading of " + GlobalVariable.Getstrpath()
                                + " complete.", Toast.LENGTH_LONG).show();
    //          Intent i = new Intent(FindFilesByType.this, SongList.class);
    //          finish();
    //          startActivity(i);
                try{
                Intent i = new Intent(FindFilesByType.this, SongList.class);
                finish();
                startActivity(i);}catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }

the entiere code works fine for emulator

but it always doesn't display me progress but download happens sucessfully

thanks Pragna


Somethimes java can't retrive the size of a remote file, and i think that is your problem. But please check internet connection on your phone when debugging the code.


Got the same problem (on Android 4.0 only), and removing

c.setDoOutput(true);

fixed it for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜