使用编码的android我怎样才能设置壁纸? [英] How can i set Wallpaper in android using coding?

查看:209
本文介绍了使用编码的android我怎样才能设置壁纸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发,显示来自服务器和用户的不同的照片可以将选定的照片作为其设备我以前给code设置壁纸它的工作,但图像没有正确设置它不适合屏幕的墙纸应用程序。我用这个code。

I am developing an application that shows the different photos from server and user can set selected photos as wallpaper of its device i used given code to set wallpaper it working but image was not set properly it not fit to screen. I used this code.

            String dirPath = getFilesDir().toString();

        String folder = mPhotos.get(nextPosition - 1).getCategory();
        String filePath = dirPath + "/PhotoViewer/" + folder + "/"
                + mPhotos.get(nextPosition - 1).getFileName();
        File imageFile = new File(filePath);
        Bitmap bitmap = BitmapFactory.decodeFile(imageFile
                .getAbsolutePath());
        WallpaperManager myWallpaperManager = WallpaperManager
                .getInstance(getApplicationContext());
        try {
            myWallpaperManager.setBitmap(bitmap);
            Toast.makeText(PhotoActivity.this, "Wallpaper set",
                    Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(PhotoActivity.this, "Error setting wallpaper",
                    Toast.LENGTH_SHORT).show();
        }

请帮我了。

推荐答案

要设置的墙纸在下面code的android应用:通过使用WallpaperManager类

To set wallpaper in android use below code: By using WallpaperManager Class

Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.preview);
imagePreview.setImageResource(R.drawable.five);

buttonSetWallpaper.setOnClickListener(new Button.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        WallpaperManager myWallpaperManager 
        = WallpaperManager.getInstance(getApplicationContext());
        try {
            myWallpaperManager.setResource(R.drawable.five);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});

需要在设置权限清单:

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

这篇关于使用编码的android我怎样才能设置壁纸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆