Blog.java (4632B)
1 package de.openrat.android.blog; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 7 import de.openrat.android.blog.service.MailIntentService; 8 import de.openrat.android.blog.service.UploadIntentService; 9 10 import android.app.Activity; 11 import android.content.Context; 12 import android.content.Intent; 13 import android.content.SharedPreferences; 14 import android.graphics.Bitmap; 15 import android.net.Uri; 16 import android.os.Bundle; 17 import android.os.Environment; 18 import android.preference.PreferenceManager; 19 import android.provider.MediaStore; 20 import android.view.Menu; 21 import android.view.MenuInflater; 22 import android.view.MenuItem; 23 import android.view.View; 24 import android.view.View.OnClickListener; 25 import android.widget.Button; 26 import android.widget.EditText; 27 import android.widget.ImageView; 28 29 public class Blog extends Activity 30 { 31 private static final int TAKE_PHOTO_CODE = 1; 32 private String imageFilename; 33 private ImageView image; 34 private EditText title; 35 private EditText text; 36 private Button button; 37 38 @Override 39 public void onCreate(Bundle savedInstanceState) 40 { 41 42 super.onCreate(savedInstanceState); 43 setContentView(R.layout.main); 44 45 title = (EditText) findViewById(R.id.title); 46 text = (EditText) findViewById(R.id.text); 47 image = (ImageView) findViewById(R.id.image); 48 image.setOnClickListener(new OnClickListener() 49 { 50 51 @Override 52 public void onClick(View v) 53 { 54 takePhoto(); 55 } 56 }); 57 button = (Button) findViewById(R.id.save); 58 button.setOnClickListener(new OnClickListener() 59 { 60 61 @Override 62 public void onClick(View v) 63 { 64 SharedPreferences prefs = PreferenceManager 65 .getDefaultSharedPreferences(Blog.this); 66 67 if (prefs.getBoolean("use_upload", false)) 68 { 69 final Intent uploadIntent = new Intent(Blog.this, 70 UploadIntentService.class); 71 72 uploadIntent.putExtra(UploadIntentService.EXTRA_TITLE, 73 title.getText()); 74 uploadIntent.putExtra(UploadIntentService.EXTRA_TEXT, text 75 .getText()); 76 uploadIntent.putExtra(UploadIntentService.EXTRA_IMAGE, 77 imageFilename); 78 startService(uploadIntent); 79 80 } 81 if (prefs.getBoolean("use_email", false)) 82 { 83 final Intent mailIntent = new Intent(Blog.this, 84 MailIntentService.class); 85 mailIntent.putExtra(UploadIntentService.EXTRA_TITLE, title 86 .getText()); 87 mailIntent.putExtra(UploadIntentService.EXTRA_TEXT, text 88 .getText()); 89 mailIntent.putExtra(UploadIntentService.EXTRA_IMAGE, 90 imageFilename); 91 92 startService(mailIntent); 93 } 94 95 } 96 }); 97 } 98 99 @Override 100 public boolean onCreateOptionsMenu(Menu menu) 101 { 102 super.onCreateOptionsMenu(menu); 103 MenuInflater mi = new MenuInflater(getApplication()); 104 mi.inflate(R.menu.main, menu); 105 106 return true; 107 } 108 109 public boolean onOptionsItemSelected(MenuItem item) 110 { 111 switch (item.getItemId()) 112 { 113 case R.id.menu_preferences: 114 startActivity(new Intent(this, PreferencesActivity.class)); 115 return true; 116 case R.id.menu_take_photo: 117 takePhoto(); 118 return true; 119 } 120 return false; 121 } 122 123 /* 124 * 125 * Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { 126 * public void onPictureTaken(byte[] imageData, Camera c) { 127 * 128 * if (imageData != null) { 129 * 130 * Intent mIntent = new Intent(); 131 * 132 * FileUtilities.StoreByteImage(mContext, imageData, 50, "ImageName"); 133 * mCamera.startPreview(); 134 * 135 * setResult(FOTO_MODE,mIntent); finish(); 136 * 137 * 138 * } } }; 139 */ 140 141 private void takePhoto() 142 { 143 final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 144 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri 145 .fromFile(getTempFile(this))); 146 startActivityForResult(intent, TAKE_PHOTO_CODE); 147 } 148 149 private File getTempFile(Context context) 150 { 151 // it will return /sdcard/image.tmp 152 final File path = new File(Environment.getExternalStorageDirectory(), 153 context.getPackageName()); 154 if (!path.exists()) 155 { 156 path.mkdir(); 157 } 158 return new File(path, "image.tmp"); 159 } 160 161 @Override 162 protected void onActivityResult(int requestCode, int resultCode, Intent data) 163 { 164 if (resultCode == RESULT_OK) 165 { 166 switch (requestCode) 167 { 168 case TAKE_PHOTO_CODE: 169 final File file = getTempFile(this); 170 this.imageFilename = file.getAbsoluteFile().getName(); 171 try 172 { 173 Bitmap captureBmp = android.provider.MediaStore.Images.Media 174 .getBitmap(getContentResolver(), Uri 175 .fromFile(file)); 176 177 image.setImageBitmap(captureBmp); 178 } 179 catch (FileNotFoundException e) 180 { 181 e.printStackTrace(); 182 } 183 catch (IOException e) 184 { 185 e.printStackTrace(); 186 } 187 break; 188 } 189 } 190 } 191 192 }