【问题标题】:How to set image as a wallpaper on a single home screen如何将图像设置为单个主屏幕上的壁纸
【发布时间】:2012-11-01 11:19:45
【问题描述】:

我们知道,android 带有 3 个或 5 个或 7 个主屏幕。我想以编程方式在单个主屏幕上设置壁纸图像并为所有其他主屏幕修复它。

我使用了以下代码:

 Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
 Bitmap bmp = Bitmap.createScaledBitmap(wallpaperImage,display.getWidth(),display.getHeight(), true);
 setWallpaper(bmp);

并且设置了壁纸图像,但它跨越了所有 3 个主屏幕。如何将其固定到一个屏幕上,以便在用户切换主屏幕时显示单个图像。

【问题讨论】:

    标签: android wallpaper


    【解决方案1】:
    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();
        }
    } 
    

    打开Androidmanifest.xml文件,添加使用权限为

    'uses-permission android:name="android.permission.SET_WALLPAPER" /'
    

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

    【讨论】:

      【解决方案2】:

      通过使用下面的代码你可以设置

          Uri sendUri = Uri.fromFile(externalFile)
          Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
          intent.setDataAndType(sendUri, "image/jpg");
          intent.putExtra("mimeType", "image/jpg");
          startActivityForResult(Intent.createChooser(intent, "Set As"), 200);
      

      并在 androidmanifest.xml 中添加权限

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多