android - save image from web server and set it as wallpaper
Asked Answered
B

1

3

Can anyone please provide me some idea/guidance on how to save an image from a webserver and set it as wallpaper? i am developing an android application which needs to do that and i am new in android. Thanks a lot.

I had tried writing my own code but it doesn't work as i can't find my images after download but the wallpaper has change to the downloaded picture. here is my existing code.

Bitmap bmImg;

void downloadFile(String fileUrl) {
    URL myFileUrl = null;
    try {
        myFileUrl = new URL(fileUrl);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl
                .openConnection();
        conn.setDoInput(true);
        conn.connect();
        int length = conn.getContentLength();

        InputStream is = conn.getInputStream();

        bmImg = BitmapFactory.decodeStream(is);
        // this.imView.setImageBitmap(bmImg);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        String filepath=Environment.getExternalStorageDirectory().getAbsolutePath(); 
        FileOutputStream fos = new FileOutputStream(filepath + "/" + "output.jpg"); 
        bmImg.compress(CompressFormat.JPEG, 75, fos);
        fos.flush();
        fos.close();

        Context context = this.getBaseContext();
        context.setWallpaper(bmImg);
    } catch (Exception e) {
        //Log.e("MyLog", e.toString());
        TextView tv = (TextView) findViewById(R.id.txt_name);
        tv.setText(e.toString());
    }

}
Birdt answered 20/1, 2010 at 1:49 Comment(0)
T
2

I had tried writing my own code but it doesn't work as i can't find my images after download. here is my existing code.

Your code would save the image in the data/data/<your_app_package_name> folder of the phone. You can then use either a WallpaperManager instance or do a context.setWallpaper(bitmap)(this is deprecated) to set your bitmap as the wallpaper.

Techy answered 20/1, 2010 at 4:37 Comment(4)
I would like to store the image in the sdCardBirdt
Seems like you've changed a few lines in your code: this should now save the image on the Sdcard. And also, set it as wallpaper. Are you getting any errors?Techy
yup.. i had make some changes to my code. i did not receive any error but i still could not find the image in the phone. however the wallpapers had successfully set.Birdt
hi Samuh, just to inform you that i got it on the sdcard. Thanks a lot for your prompt answer.Birdt

© 2022 - 2024 — McMap. All rights reserved.