【问题标题】:android - save image from web server and set it as wallpaperandroid - 从网络服务器保存图像并将其设置为壁纸
【发布时间】:2011-01-07 02:23:23
【问题描述】:

谁能提供一些关于如何从网络服务器保存图像并将其设置为壁纸的想法/指导?我正在开发一个需要这样做的android应用程序,我是android的新手。非常感谢。

我曾尝试编写自己的代码,但它不起作用,因为我在下载后找不到我的图片,但壁纸已更改为下载的图片。这是我现有的代码。

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());
    }

}

【问题讨论】:

    标签: android image save set wallpaper


    【解决方案1】:

    我曾尝试编写自己的代码,但它 不起作用,因为我找不到我的图像 下载后。这是我现有的 代码。

    您的代码会将图像保存在手机的data/data/<your_app_package_name> 文件夹中。然后,您可以使用WallpaperManager instancecontext.setWallpaper(bitmap)(已弃用)将位图设置为墙纸。

    【讨论】:

    • 我想将图像存储在 sdCard 中
    • 似乎您已经更改了代码中的几行:现在应该将图像保存在 SD 卡上。而且,将其设置为墙纸。你有任何错误吗?
    • 是的..我对我的代码做了一些更改。我没有收到任何错误,但我仍然无法在手机中找到图像。但是壁纸已成功设置。
    • 嗨 Samuh,只是为了通知你我在 sdcard 上得到了它。非常感谢您的及时答复。
    猜你喜欢
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多