【问题标题】:Trouble downloading photos无法下载照片
【发布时间】:2019-07-02 14:48:56
【问题描述】:

我写了一个程序 从网址下载照片... 但是我有一个问题... 部分照片已下载。 而且没有问题。

但是有些图片不能完全下载:( 在文件管理器中我查看 它被打破 你能帮忙吗?

我的代码是:

公共类 DownloadFileFromURL_img 扩展 AsyncTask {

    private viewHolderPost holderPOST;

    public DownloadFileFromURL_img(viewHolderPost holderPOST) {
        Log.d(TAG, "DownloadFileFromURL_img: ");
        this.holderPOST = holderPOST;

    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    /**
     * Downloading file in background thread
     */
    @Override
    protected String doInBackground(String... f_url) {
        int count;
        try {

            File file = new File(Environment.getExternalStorageDirectory(), "98Diha/img");

            if (!file.exists()) {
                if (!file.mkdirs()) {

                    file.createNewFile();
                }
            }



            InputStream input = null;
            int response = -1;

            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();

            if (!(conection instanceof HttpURLConnection))
                throw new IOException("Not an HTTP connection");

            try{
                HttpURLConnection httpConn = (HttpURLConnection) conection;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect();

                response = httpConn.getResponseCode();
                if (response == HttpURLConnection.HTTP_OK) {
                    input = httpConn.getInputStream();
                }
            }
            catch (Exception ex)
            {
                throw new IOException("Error connecting");
            }






            int lenghtOfFile = conection.getContentLength();



             input = new BufferedInputStream(url.openStream());


            String imgS[] = f_url[0].split("/");
            String name = imgS[imgS.length - 1];



            String path = Environment
                    .getExternalStorageDirectory().toString()
                    + "/98diha/img/" + name;


            File filePath = new File(path);

            if (!filePath.exists()) {

                OutputStream output = new FileOutputStream(path);

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;


                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));


                    output.write(data, 0, count);
                }


                output.flush();


                output.close();
                input.close();

            } else {
                SSToast(context, "Exist!");

                holderPOST.dowload_img.setVisibility(View.GONE);
                holderPOST.setWallpaper.setVisibility(View.VISIBLE);
                holderPOST.setWallpaper.setText(context.getString(R.string.set_wp));


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    holderPOST.setWallpaper.setTextColor(ContextCompat.getColor(context, R.color.Teal_400));
                } else {
                    holderPOST.setWallpaper.setTextColor(context.getResources().getColor(R.color.Teal_400));
                }
            }

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }

    /**
     * Updating progress bar
     */
    protected void onProgressUpdate(String... progress) {

        holderPOST.dowload_img.setVisibility(View.GONE);
        holderPOST.setWallpaper.setVisibility(View.VISIBLE);
        holderPOST.setWallpaper.setText(context.getString(R.string.dowloading));


    }

    /**
     * After completing background task Dismiss the progress dialog
     **/
    @Override
    protected void onPostExecute(String file_url) {


        holderPOST.setWallpaper.setText(context.getString(R.string.set_wp));
        holderPOST.setWallpaper.setTextColor(context.getResources().getColor(R.color.Teal_400));

        Log.d(TAG, "onPostExecute: ");
    }

}

【问题讨论】:

    标签: java android image android-asynctask urlconnection


    【解决方案1】:

    而不是使用AsyncTask 从 URL 下载图像。您可以使用诸如GlidePicasso 之类的库在一行中快速完成。但是,如果您不想使用库,则可以使用DownloadManager 下载它并将其保存在文件中。您可以查看tutorial 或网上其他教程了解 DownloadManager 的实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多