import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
WallpaperManager wallpaperManager;
Drawable wallpaperDrawable;
WallpaperManager wallpaperManager2;
Drawable wallpaperDrawable2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
saveImageToInternalStorage(drawableToBitmap(wallpaperDrawable),
"temp_original_wallpaper");
Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivityForResult(
Intent.createChooser(intent, "Select Wallpaper"), 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
saveImageToInternalStorage(drawableToBitmap(wallpaperDrawable),
"temp_second_wallpaper");
WallpaperManager wpm = WallpaperManager.getInstance(getBaseContext());
try {
wpm.setBitmap(getThumbnail("temp_original_wallpaper.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean saveImageToInternalStorage(Bitmap image, String filename) {
try {
// Use the compress method on the Bitmap object to write image to
// the OutputStream
FileOutputStream fos = openFileOutput(filename + ".png",
Context.MODE_PRIVATE);
// Writing the bitmap to the output stream
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
return true;
} catch (Exception e) {
// Log.e("saveToInternalStorage()", e.getMessage());
return false;
}
}
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
public Bitmap getThumbnail(String filename) {
String fullPath = getFilesDir().getAbsolutePath() ;
Bitmap thumbnail = null;
// Look for the file on the external storage
try {
thumbnail = BitmapFactory.decodeFile(fullPath + "/" + filename);
} catch (Exception e) {
//Log.e("getThumbnail() on external storage", e.getMessage());
}
// If no file on external storage, look in internal storage
if (thumbnail == null) {
try {
File filePath = getFileStreamPath(filename);
FileInputStream fi = new FileInputStream(filePath);
thumbnail = BitmapFactory.decodeStream(fi);
} catch (Exception ex) {
//Log.e("getThumbnail() on internal storage", ex.getMessage());
}
}
return thumbnail;
}
}