1

就像我在前一个问题中写的那样,我写的 AsyncTask 的子类没有按预期下载文件。

public class Upit extends DialogFragment {

    public ProgressDialog dl;

    private class DownloadFile extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... sUrl) {
            try {
                URL url = new URL(sUrl[0]);
                URLConnection urlc = url.openConnection();
                urlc.connect();
                int fileLength = urlc.getContentLength();
                InputStream is = new BufferedInputStream(url.openStream());
                OutputStream os = new FileOutputStream(
                        Environment.getDataDirectory().getAbsolutePath()
                                .concat("data/com.markosoft.maturko/databases/proba.mp3"));
                byte Data[] = new byte[1024];
                long total = 0;
                int count;
                while ((count = is.read(Data)) != -1) {
                    total += count;
                    publishProgress((int) total * 100 / fileLength);
                    os.write(Data, 0, count);
                }
                os.flush();
                os.close();
                is.close();
            } catch (Exception e) {
                Log.e("ERROR_TAG", e.getMessage());
                return "Podaci neuspešno preuzeti";
            }
            return "Podaci uspešno preuzeti";
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            dl.setProgress(progress[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(dl.getContext(), result, Toast.LENGTH_SHORT).show();
            dl.dismiss();
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanteState) {
        AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
        b.setMessage(R.string.upit_poruka)
                .setPositiveButton(R.string.upit_da, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        if (Provera()) {
                            upd(getActivity());
                        }
                        Izadji();
                    }
                })
                .setNegativeButton(R.string.upit_ne, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        Izadji();
                    }
                });
        return b.create();
    }

    protected void Izadji() {
        this.dismiss();
    }

    protected boolean Provera() {
        return true;
    }

    public void upd(Context context) {
        dl = new ProgressDialog(context);
        dl.setMessage("Preuzimanje najnovijih podataka...");
        dl.setIndeterminate(false);
        dl.setMax(100);
        dl.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dl.setCancelable(true);
        dl.show();
        DownloadFile dlf = new DownloadFile();
        dlf.execute("http://podcasts.aolcdn.com/massively/podcasts/03-19-13-massively-E241.mp3");
    }

    @Override
    public void onStop() {
        super.onStop();
    }
}

问题很可能出在文件的路径中。顺便说一句,我已经请求许可AndroidManifest.xml

4

0 回答 0