下载和设置壁纸 [英] Downloading and setting a wallpaper

查看:120
本文介绍了下载和设置壁纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这小片code的,我想实现这一点:程序应该从设置链接的图像壁纸

I have this little piece of code and I want to achieve this: program should set a wallpaper from linked image.

ImgDownload:

public class ImgDownload extends AsyncTask {
        private String requestUrl;
        private ImageView view;
        private Bitmap pic;

        private ImgDownload(String requestUrl, ImageView view) {
            this.requestUrl = requestUrl;
            this.view = view;
        }

        @Override
        protected Object doInBackground(Object... objects) {
            try {
                URL url = new URL(requestUrl);
                URLConnection conn = url.openConnection();
                pic = BitmapFactory.decodeStream(conn.getInputStream());
            } catch (Exception ex) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            view.setImageBitmap(pic);
        }
    }

public class MainActivity extends Activity {

    private ImageView img;

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

//!!!! This is where I am stuck :)
        Object s = new ImgDownload("http://images1.wikia.nocookie.net/__cb20120402213849/masseffect/images/4/42/Uncharted_Worlds_Codex_Image.jpg",img );
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

如何实例/在我的mainActivity创建这个类,所以它可以从链接下载IMG?任何帮助的建议,想法,将AP preciated:)

How to instantiate/create this class in my mainActivity, so it could download img from link? Any help suggestions, thoughts, will be appreciated :)

推荐答案

您执行此AsyncTask的是这样的:

You execute this AsyncTask like this:

ImgDownload downloader = new ImgDownload("http://images1.wikia.nocookie.net/__cb20120402213849/masseffect/images/4/42/Uncharted_Worlds_Codex_Image.jpg",img);
downloader.execute();

不过,我不会建议使用code,因为它会产生内存泄漏。例如尝试,而它正在下载一个图像旋转您的设备。我向你保证你的应用程序会崩溃。加上AsyncTask的是一个通用类。你可以用它来使你的code稍微简单一些。这里是我的提高图像的下载任务:

But I would not recommend using your code as it will produce memory leaks. For example try to rotate your device while it is downloading an image. I guarantee you your application will crash. Plus AsyncTask is a generic class. You could use that to make your code a little simpler. Here is my improved image download task:

public class ImgDownload extends AsyncTask<Void, Void, Bitmap> { // Use Generics
    private final String requestUrl;
    private final WeakReference<ImageView> imageViewReference; // Use WeakReference to prevent memory leaks

    public ImgDownload(String requestUrl, ImageView view) {
        this.requestUrl = requestUrl;
        this.imageViewReference = new WeakReference<ImageView>(view);
    }

    @Override
    protected Bitmap doInBackground(Void... objects) {
        try {
            URL url = new URL(requestUrl);
            URLConnection conn = url.openConnection();
            return BitmapFactory.decodeStream(conn.getInputStream()); // Return bitmap instead of using global variable
        } catch (Exception ex) {
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        ImageView imageView = imageViewReference.get();
        if(imageView != null && bitmap != null) { // Check if image or ImageView are null
            imageView.setImageBitmap(bitmap);
        }
    }
} 

这篇关于下载和设置壁纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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