Search code examples
androidasynchronousandroid-asynctaskimageviewwallpaper

Android AsyncTask using Wallpaper Manager


My wallpaper manager uses a Async Task to set as wallpaper. But after changing to AsyncTask from non-Async I got the error "The method getBaseContext() is undefined for the type SetWallpaperTask" Please correct my codes. Thank you very much.

Old Non-AsyncTask

public void SetWallpaper(String image_url)
        {   
        URL myFileUrl = null;
            try
        {   
                myFileUrl = new URL(image_url); 
        }
            catch (MalformedURLException e)
            {      
                e.printStackTrace();  
            }   
            Bitmap bmImg = null;
            try {  
                HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();   
                conn.setDoInput(true);   
                conn.connect();     
                //int length = conn.getContentLength(); 
            InputStream is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is); 
            }
                catch (IOException e)
                {       
                    e.printStackTrace();  
                }   
        try {       

            String path = myFileUrl.getPath();
            String idStr = path.substring(path.lastIndexOf('/') + 1);
            File filepath = Environment.getExternalStorageDirectory();
            File dir = new File (filepath.getAbsolutePath() + "/Wallpaper/");
                dir.mkdirs();
                String fileName = idStr;
                File file = new File(dir, fileName);
                FileOutputStream fos = new FileOutputStream(file);

                bmImg.compress(CompressFormat.JPEG, 75, fos);   
                fos.flush();    
                fos.close();       

                WallpaperManager wpm = WallpaperManager.getInstance(getBaseContext());
                wpm.setBitmap(bmImg);

        }
        catch (Exception e){


            e.printStackTrace();  
        }
        }

New AsyncTask

public class SetWallpaperTask extends AsyncTask<String , String , String>
{
    private Context context;
    private ProgressDialog pDialog;
    String image_url;
    URL myFileUrl;
    String myFileUrl1;
    Bitmap bmImg = null;

    public SetWallpaperTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub

        super.onPreExecute();

        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Downloading Image ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub

        try {  

            myFileUrl = new URL(args[0]);
            //myFileUrl1 = args[0];

            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();   
            conn.setDoInput(true);   
            conn.connect();     
            InputStream is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is); 
        }
        catch (IOException e)
        {       
            e.printStackTrace();  
        }
        try {       

            String path = myFileUrl.getPath();
            String idStr = path.substring(path.lastIndexOf('/') + 1);
        File filepath = Environment.getExternalStorageDirectory();
        File dir = new File (filepath.getAbsolutePath() + "/Wallpaper/");
            dir.mkdirs();
            String fileName = idStr;
            File file = new File(dir, fileName);
            FileOutputStream fos = new FileOutputStream(file);
            bmImg.compress(CompressFormat.JPEG, 75, fos);   
            fos.flush();    
            fos.close();    

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


    @Override
    protected void onPostExecute(String args) {
        // TODO Auto-generated method stub
        WallpaperManager wpm = WallpaperManager.getInstance(context()); // --The method context() is undefined for the type SetWallpaperTask
        wpm.setBitmap(bmImg);
        pDialog.dismiss();

    }


}

Solution

  • This is because you cannot get a Context inside an AsyncTask using getBaseContext().

    I see that you're already receiving a Context in your constructor and storing it in the class variable context. So you can simply change

        WallpaperManager wpm = WallpaperManager.getInstance(getBaseContext()); 
    

    to

        WallpaperManager wpm = WallpaperManager.getInstance(context);