Pregunta

I'm kind of new in android developing and i want to set home wallpaper with drawable image . i wrote this code for setting but don't work and in visual android device at first i have dialog but next when i click as background i get this message :

"you need to insert sd card before using camera ..."

also i need use my drawable as source of images ... here is my code :

Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
MimeTypeMap map = MimeTypeMap.getSingleton();
String mimeType = map.getMimeTypeFromExtension("jpg");    
 Uri uri = Uri.parse("@drawable/wall_7");         
intent.setDataAndType(uri,"image/jpeg" );
intent.putExtra("mimeType", mimeType);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);      
this.startActivityForResult(Intent.createChooser(intent,"Set as"),200);

and i going to have a slidable wallpaper , not a fix image . i'm using this intent bcs it let user crop image .

¿Fue útil?

Solución

First of all you need to add this permession to your manifest.xml

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

Then you can use this to set it using the wallpaperManager :

WallpaperManager wallpaperManager = WallpaperManager.getInstance(MyActivity.this); 
Drawable drawable = getResources().getDrawable(R.drawable.wallpaper_img);
                Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                wallpaperManager.setBitmap(bitmap);

Or you can use this to set the wallpaper using intent

Otros consejos

You can directly set background in your xml file using android:background tag.Let me know if you have any further queries.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top