2013年11月28日 星期四

save old wallpaper & selete new wallpaper & save & set the old one

package com.example.testgetwallpaper;

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;
}
}

get wallpaper drawable

WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Drawable wallpaperDrawable = wallpaperManager.getDrawable();

2013年11月4日 星期一

Screen Timeout

public class MainActivity extends Activity {
int defTimeOut = 0;
private static final int DELAY = 30*1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

defTimeOut = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, DELAY);
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, DELAY);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

drawable To Bitmap


2 
Hi Praveen. The drawable I want to set as the wallpaper is not in R.drawable. It is a drawable I downloaded from the web and keep it in an arrayList of type Drawable along with other drawables. –  Rob Jun 14 '10 at 8:37 
1 
i think you have the url values. then my edited answer should help. –  Praveen Jun 14 '10 at 8:49
 
where does str_url come from? I couldn't find any Drawable function related to strings... thanks for your help.–  Rob Jun 14 '10 at 9:08
4 
I think I found something: if "draw" is the drawable I want to convert to a bitmap then: Bitmap bitmap = ((BitmapDrawable)draw).getBitmap(); does the trick! –  Rob Jun 14 '10 at 9:29 
1 
@Rob : if your Drawable is a BitmapDrawable only. (which means that your Drawable is but a wrapper around a Bitmap, actually) –  njzk2 May 28 at 11:47
show 1 more comments

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;
}
share|improve this answer
8 
This looks like the only answer that would work for any kind of drawable and also has a quick solution for a drawable that is already a BitmapDrawable. +1 –  Matt Wolfe Jun 6 '12 at 22:37
 
Add @Mauro's solution to this after the check for BitmapDrawable and you've got a very ideal solution with any/ all quicker solutions Incorporated. –  Tom Jul 4 '12 at 23:45
 
This solution works even if the drawable is a 9 patch image. –  Prakash Nadar Aug 15 '12 at 18:47
 
just one amendment: docs says about BitmapDrawable.getBitmap() that it may come back null. I say it may also come back already recycled. –  kellogs Sep 1 '12 at 21:20
7 
Watch out: getIntrinsicWidth() and getIntrinsicHieght() will return -1 if drawable is a solid color.–  S.D. Oct 12 '12 at 13:51
Related Posts Plugin for WordPress, Blogger...