Wednesday, March 2, 2016

Get wallpaper using WallpaperManager


Example to retrieve the current system wallpaper by calling getDrawable() method of WallpaperManager; if no wallpaper is set, the system built-in static wallpaper is returned.

package com.blogspot.android_er.androidwallpaper;

import android.app.WallpaperManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView image = (ImageView)findViewById(R.id.image);
        WallpaperManager myWallpaperManager =
                WallpaperManager.getInstance(getApplicationContext());
        Drawable drawableWallpaper = myWallpaperManager.getDrawable();
        image.setImageDrawable(drawableWallpaper);

        Toast.makeText(this,
                drawableWallpaper.getMinimumWidth() + " x " + drawableWallpaper.getMinimumHeight()
                + "\n" +
                drawableWallpaper.getIntrinsicWidth() + " x " + drawableWallpaper.getIntrinsicHeight(),
                Toast.LENGTH_LONG).show();
    }
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context="com.blogspot.android_er.androidwallpaper.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>



NextL
- Load photo and set Wallpaper

No comments: