使用URL下载图像 [英] Dowloading Image using URL

查看:91
本文介绍了使用URL下载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的应用中的网址和按钮下载图片。当我在手机上运行时,我无法下载图像。任何人都可以指出这个问题。感谢您的帮助:)

I'm trying to download an image using a URL and a button in my app. When I'm running it on my phone, I,m not able to download the image. Can anyone please point out the problem with this. Thanks for the help in advance :)

这是我的代码。

public class MainActivity extends AppCompatActivity {

ImageView download;

public void downloadImage(View view){

    DownloadImage image = new DownloadImage();
    Bitmap result;
    try {
        result = image.execute("https://en.wikipedia.org/wiki/File:Bart_Simpson_200px.png").get();
        download.setImageBitmap(result);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    download = (ImageView) findViewById(R.id.imageView);
}

public class DownloadImage extends AsyncTask<String, Void, Bitmap>{

    @Override
    protected Bitmap doInBackground(String... urls) {
        URL url;
        HttpURLConnection urlConnection = null;
        try {
            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoInput(true);
            urlConnection.connect();
            InputStream in = urlConnection.getInputStream();
            Bitmap Image = BitmapFactory.decodeStream(in);
            in.close();
            return Image;
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
         return null;
    }
}
}


推荐答案

您可以通过两种方式从网址下载图片

you can download image from url in two ways

1 。您可以使用 Glide library 从url加载图片看下面的代码,它可以用简单的方式帮助你

1.you can user Glide library to load image from url look the below code it can help you in simple way

编译此库

   implementation 'com.github.bumptech.glide:glide:4.6.1'

比像这样加载图片

 Glide.with(MainActivity.this)
        .load(url)
        .apply(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
        .into(imageview);

2。如果您不想使用第三方库 <。 / p>

2 .try this if you dont want to use third party library

 new DownloadImage(imamgeview).execute(url);

创建异步任务

public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImage(ImageView bmImage) {
    this.bmImage = (ImageView ) bmImage;
}

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.d("Error", e.getStackTrace().toString());

    }
    return mIcon11;
}

protected void onPostExecute(Bitmap result) {
    bmImage.setImageBitmap(result);
}
}

我希望它适用于您的情况

i hope that it will work in your case

这篇关于使用URL下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆