Android-下载文件时,水平进度栏不起作用

米特什·沙(Mitesh Shah)

我正在研究一个项目,并且单击并ListView同时显示时正在下载.pdf文件horizontal progress bar。.但是,它停止在1%位置并且没有前进,并且也没有下载该文件。

请检查代码..我缺少了什么??

代码

public class Downloads_screen extends Activity
{

ProgressDialog mProgressDialog;

String mPdf_links;
String mlist;
String img_path;

@Override
protected void onCreate(Bundle savedInstanceState)
    {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.downloads_screen);

    Intent i = getIntent();
    mlist = i.getExtras().getString("TITLE");
    mPdf_links = i.getExtras().getString("PDFPATH");
    img_path = i.getExtras().getString("IMGPATH");

    Log.v("Download Page", "Title: "+mlist);
    Log.v("Download Page", "Pdf Path: "+mPdf_links);

    RelativeLayout backbutton = (RelativeLayout) findViewById(R.id.download_screen_backbutton);

    backbutton.setOnClickListener(new OnClickListener()
              {
        @Override
        public void onClick(View v)
               {
            // TODO Auto-generated method stub
            finish();
        }
    });

    Boolean isFilePresent;

          String filename = mPdf_links.substring(mPdf_links.lastIndexOf('/') + 1,                mPdf_links.length());
        File downloadfile = new File("/sdcard/CONSTRUCTION/" + filename);
        if (downloadfile.exists()) {
            isFilePresent = true;
        } else {
            isFilePresent= false;
        }
        System.out.println(filename + ":" + isFilePresent);


    MyDownload_adapter adapter = new MyDownload_adapter(Downloads_screen.this, mlist, img_path, isFilePresent);
    ListView downloadlist = (ListView) findViewById(R.id.download_screen_listView1);
    downloadlist.setAdapter(adapter);
    downloadlist.setOnItemClickListener(new OnItemClickListener() {

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @SuppressLint("NewApi")
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            // instantiate it within the onCreate method
            String filename = mPdf_links.substring(
                    mPdf_links.lastIndexOf('/') + 1,
                    mPdf_links.length());
            System.out.println("FileName:" + filename);
            File downloadfile = new File("/sdcard/CONSTRUCTION/" + filename);
            if (!downloadfile.exists()) {
                mProgressDialog = new ProgressDialog(Downloads_screen.this);
                mProgressDialog.setMessage("Downloading..");
                mProgressDialog.setIndeterminate(false);
                mProgressDialog.setProgress(0);
                mProgressDialog.incrementProgressBy(1);
                mProgressDialog.setMax(100);
                mProgressDialog
                        .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

                // execute this when the downloader must be fired
                DownloadFile downloadFile = new DownloadFile();
                System.out.println(mPdf_links);
                downloadFile.execute(mPdf_links);

            } else {
                File file = new File(Environment
                        .getExternalStorageDirectory().getAbsolutePath()
                        + "/CONSTRUCTION/" + filename);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file), "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);

            }
        }
    });

}

private class DownloadFile extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... sUrl) {
        try {
            URL url = new URL(sUrl[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            // this will be useful so that you can show a typical 0-100%
            // progress bar
            int fileLength = connection.getContentLength();
            System.out.println("File length:" + fileLength);
            System.out.println("TEST here in Downloadfile");
            // download the file
            File wallpaperDirectory = new File("/sdcard/CONSTRUCTION/");
            // have the object build the directory structure, if needed.
            wallpaperDirectory.mkdirs();
            // create a File object for the output file

            String filename = sUrl[0].substring(
                    sUrl[0].lastIndexOf('/') + 1, sUrl[0].length());
            System.out.println("FileName:" + filename);
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(
                    "/sdcard/CONSTRUCTION/" + filename);

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                System.out.println("Total downloaded" + total + " count:"
                        + count);
                // publishing the progress....
                publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
            mProgressDialog.dismiss();
            File file = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/CONSTRUCTION/" + filename);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);

        } catch (Exception e) {
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        mProgressDialog.setProgress(progress[0]);
    }
}
}
米特什·沙(Mitesh Shah)

我上面的代码可以正常工作..

唯一的问题是我获取的文件名不正确,即它也具有特殊字符和空格,下载程序也无法获取它,因此下载未启动。

只需告诉您的web_dept人员将文件重命名为正确的格式即可。否则您必须使用字符串操作来正确地重命名文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Android下载文件,并在ProgressDialog中显示进度

Android:底部导航顶部的浮动操作栏不起作用

下载文件并保存到光盘不起作用

android:fitsSystemWindows在工具栏上不起作用

如何在Android中更改进度栏的颜色?-(我尝试了一种方法,但不起作用)

通过Android Retrofit 2下载文件时是否可以显示进度栏

下载文件链接在Angular中不起作用

在div中加载时,加载进度栏不起作用

从Firebase存储下载文件不起作用

在nwjs中下载文件时如何显示进度栏

下载文件时如何显示进度栏?

登录时VBA下载文件不起作用

在DownloadManager上下载并在setDestinationInExternalFilesDir()中保存文件在Android 10上不起作用

从Yahoo Finance下载并解析csv文件在我的Android应用程序上不起作用

从WebClient下载文件不起作用C#

Android操作栏搜索不起作用

Android设置动作栏的文本颜色,示例不起作用

Android下载文件损坏

VBA从Internet WinHttpReq下载文件,登录不起作用

Android工具栏操作图标不起作用

Click事件在android操作栏项中不起作用

下载文件夹不起作用

Android Studio状态栏着色不起作用

bash下载文件不起作用

android - 当我在操作栏上添加溢出菜单时,操作向上按钮不起作用

文件上传不起作用android

Android studio 文件写入不起作用

从 Artifactory 下载文件的 Java 代码不起作用

PhpSpreadsheet 强制下载文件不起作用 - 格式错误