【问题标题】:Automatically download image from server自动从服务器下载图像
【发布时间】:2016-05-29 11:32:17
【问题描述】:

如何在不按 Android Studio 按钮的情况下从服务器下载图像。代码运行正常,但前提是我按下按钮下载。如何让图片自动下载

@覆盖 public void onClick(查看视图){ 开关(view.getId()){ 案例 R.id.bDownloadImage: 新的 DonwloadImage(downloadImageName.getText().toString()).execute(); 休息; } }

private class DonwloadImage extends AsyncTask<Void, Void, Bitmap> {
    String name;
    ProgressDialog loading;
    public DonwloadImage (String name){//constractor
        this.name = name;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(Receiver.this);
        mProgressDialog.setTitle("Downloading Image");   // title of progress dialog
        mProgressDialog.setMessage("Loading...");    // message displaying
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();    // show method
    }


    @Override
    protected Bitmap doInBackground(Void... voids) {
        // loading.dismiss();
        String url = SERVER_ADDRESS + "pictures/" + name + ".JPG";
        try
        {
            URLConnection connection = new URL(url).openConnection();
            connection.setConnectTimeout(1000 * 30);
            connection.setReadTimeout(1000 * 30);

            return BitmapFactory.decodeStream((InputStream) connection.getContent(), null, null);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }


    @Override
    protected void onPostExecute(Bitmap bitmap)
    {
        super.onPostExecute(bitmap);
        if(bitmap != null)
        {
            downloadImage.setImageBitmap(bitmap);
            mProgressDialog.dismiss();
        }
    }
}

【问题讨论】:

  • 第 1 步:选择需要下载的触发事件。第 2 步:在该事件发生时执行您的 AsyncTask

标签: android image download


【解决方案1】:

你应该添加

DonwloadImage(downloadImageName.getText().toString()).execute();

在Activity的onCreate中,为autoDownload属性。

【讨论】:

  • 是的,但还必须添加。新的 DonwloadImage(downloadImageName.getText().toString()).execute(); .谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-21
相关资源
最近更新 更多