1 package js.kbars; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.graphics.Bitmap; 6 import android.graphics.Bitmap.CompressFormat; 7 import android.graphics.BitmapFactory; 8 import android.graphics.drawable.BitmapDrawable; 9 import android.net.Uri; 10 import android.util.Log; 11 import android.view.Menu; 12 import android.view.MenuItem; 13 import android.view.MenuItem.OnMenuItemClickListener; 14 import android.view.View; 15 import java.io.File; 16 import java.io.FileNotFoundException; 17 import java.io.FileOutputStream; 18 import java.io.IOException; 19 20 public final class ImageBackgroundMenuItem implements OnMenuItemClickListener { 21 public static final int REQUEST_CODE = 234; 22 private static final String TAG = ImageBackgroundMenuItem.class.getSimpleName(); 23 private final Activity mActivity; 24 private final View mTarget; 25 26 public ImageBackgroundMenuItem(Menu menu, Activity activity, View target) { 27 this.mActivity = activity; 28 this.mTarget = target; 29 menu.add("Select image background...").setOnMenuItemClickListener(this); 30 loadBitmap(); 31 } 32 33 public boolean onMenuItemClick(MenuItem item) { 34 chooseBackground(); 35 return true; 36 } 37 38 public void onActivityResult(int resultCode, Intent data) { 39 if (resultCode == -1) { 40 Uri image = data.getData(); 41 try { 42 Bitmap bitmap = BitmapFactory.decodeStream(this.mActivity.getContentResolver().openInputStream(image)); 43 if (bitmap != null) { 44 setTargetBackground(bitmap); 45 saveBitmap(bitmap); 46 } 47 } catch (FileNotFoundException e) { 48 Log.w(TAG, "Error getting image " + image, e); 49 } 50 } 51 } 52 53 private void chooseBackground() { 54 Intent photoPickerIntent = new Intent("android.intent.action.PICK"); 55 photoPickerIntent.setType("image/*"); 56 this.mActivity.startActivityForResult(photoPickerIntent, REQUEST_CODE); 57 } 58 59 private File bitmapFile() { 60 return new File(this.mActivity.getCacheDir(), "background.png"); 61 } 62 63 private void saveBitmap(Bitmap bitmap) { 64 FileNotFoundException e; 65 Throwable th; 66 File f = bitmapFile(); 67 if (f.exists()) { 68 f.delete(); 69 } 70 if (bitmap != null) { 71 try { 72 FileOutputStream out = new FileOutputStream(f); 73 bitmap.compress(CompressFormat.PNG, 100, out); 74 out.close(); 75 } catch (IOException e1) { 76 e1.printStackTrace(); 77 } 78 } 79 } 80 81 private void loadBitmap() { 82 Bitmap bitmap = loadBitmapFromCache(); 83 if (bitmap == null) { 84 bitmap = loadBitmapFromAssets(); 85 } 86 setTargetBackground(bitmap); 87 } 88 89 private Bitmap loadBitmapFromCache() { 90 File f = bitmapFile(); 91 return f.exists() ? BitmapFactory.decodeFile(f.getAbsolutePath()) : null; 92 } 93 94 private Bitmap loadBitmapFromAssets() { 95 try { 96 return BitmapFactory.decodeStream(this.mActivity.getAssets().open("background.png")); 97 } catch (IOException e) { 98 throw new RuntimeException(e); 99 } 100 } 101 102 private void setTargetBackground(Bitmap bitmap) { 103 if (bitmap == null) { 104 this.mTarget.setBackground(null); 105 } else { 106 this.mTarget.setBackground(new BitmapDrawable(this.mActivity.getResources(), bitmap)); 107 } 108 } 109 } 110