java 如何以编程方式将图像设置为墙纸?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10160267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:52:24  来源:igfitidea点击:

How to set image as wallpaper programmatically?

javaandroidwallpaper

提问by user1166635

I have been developing the application which need to set an image as wallpaper.

我一直在开发需要将图像设置为墙纸的应用程序。

Code:

代码:

WallpaperManager m=WallpaperManager.getInstance(this);

String s=Environment.getExternalStorageDirectory().getAbsolutePath()+"/1.jpg";
File f=new File(s);
Log.e("exist", String.valueOf(f.exists()));
try {
        InputStream is=new BufferedInputStream(new FileInputStream(s));
        m.setBitmap(BitmapFactory.decodeFile(s));

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("File", e.getMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("IO", e.getMessage());
    }

Also I have added the following permission:

我还添加了以下权限:

<uses-permission android:name="android.permission.SET_WALLPAPER" />

But it doesn't works; the file exists on sdcard. Where have I made a mistake?

但它不起作用;该文件存在于 SD 卡上。我哪里做错了?

回答by mbakulin

Possibly, you run out of memory if your image is big. You can make sure of it by reading Logcat logs. If this is the case, try to adjust your picture to device size by somewhat like this:

如果图像很大,可能会耗尽内存。您可以通过阅读 Logcat 日志来确保这一点。如果是这种情况,请尝试将图片调整为设备大小,如下所示:

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels << 1; // best wallpaper width is twice screen width

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(path, options);

    WallpaperManager wm = WallpaperManager.getInstance(this);
    try {
        wm.setBitmap(decodedSampleBitmap);
    } catch (IOException e) {
        Log.e(TAG, "Cannot set image as wallpaper", e);
    }

回答by Balamurugan

File f = new File(Environment.getExternalStorageDirectory(), "1.jpg");
String path = f.getAbsolutePath();
File f1 = new File(path);

if(f1.exists()) {
    Bitmap bmp = BitmapFactory.decodeFile(path);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
    WallpaperManager m=WallpaperManager.getInstance(this);

    try {
        m.setBitmap(bmp);
    } catch (IOException e) {
        e.printStackTrace();
    }
} 

Open Androidmanifest.xml file and add permission like..

打开 Androidmanifest.xml 文件并添加权限,如..

<uses-permission android:name="android.permission.SET_WALLPAPER" />

Try this and let me know what happen..

试试这个,让我知道会发生什么..