Hey Mark (the first one),
Forgot to tell you. I fixed this problem (sort of). I got the force close to
go away but the file doesn't save.
case SAVE_MENU_ID:
mBitmap = Bitmap.createBitmap(mDrawingPanel.getWidth(),
mDrawingPanel.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(mBitmap);
mDrawingPanel.draw(canvas);
saveHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
final AlertDialog alertDialog = new AlertDialog.Builder(_panel).create();
alertDialog.setTitle("Saved 1");
alertDialog.setMessage("Your drawing had been saved :)");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
} ;
new ExportBitmapToFile(_panel, saveHandler, mBitmap);
return true;
}
return super.onOptionsItemSelected(item);
}
private class ExportBitmapToFile extends AsyncTask<Intent,Void,Boolean> {
private Context mContext;
private Handler mHandler;
private Bitmap nBitmap;
private File APP_FILE_PATH = new
File(Environment.getExternalStorageDirectory().toString() + "/DrawingPad");
public ExportBitmapToFile(Context context,Handler handler,Bitmap bitmap) {
mContext = context;
nBitmap = bitmap;
mHandler = handler;
}
@Override
protected Boolean doInBackground(Intent... arg0) {
try {
if (!APP_FILE_PATH.exists()) {
APP_FILE_PATH.mkdirs();
}
out = new FileOutputStream(new File(APP_FILE_PATH +
"/myAwesomeDrawing.png"));
nBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
return true;
}catch (Exception e) {
e.printStackTrace();
}
//mHandler.post(completeRunnable);
return false;
}
And the top of the class needs to be:
public class DrawActivity extends Activity implements
UberColorPickerDialog.OnColorChangedListener {
private Paint mPaint;
private Context _panel = this;
private int mColor = Color.BLUE;
private DrawingPanel mDrawingPanel;
Bitmap mBitmap;
Config config;
Canvas canvas;
FileOutputStream out;
Handler saveHandler;
See if you can get the file to save.
On Sat, Aug 13, 2011 at 12:25 AM, Mark Murphy <[email protected]>wrote:
> You cannot affect the UI from doInBackground(). As the stack trace
> illustrates, this includes showing Toasts. Please move your Toast to
> onPostExecute().
>
> On Fri, Aug 12, 2011 at 2:51 PM, Mark Ayers <[email protected]> wrote:
> > I'm working on a drawing app, and the code for my file saving routine
> goes
> > like this:
> > case SAVE_MENU_ID:
> > Config config = Config.ARGB_8888;
> > Bitmap mBitmap = Bitmap.createBitmap(mDrawingPanel.getWidth(),
> > mDrawingPanel.getHeight(), config);
> > Canvas canvas = new Canvas(mBitmap);
> > mDrawingPanel.draw(canvas);
> > Handler saveHandler = new Handler(){
> > @Override
> > public void handleMessage(Message msg) {
> > final AlertDialog alertDialog = new AlertDialog.Builder(_panel).create();
> > alertDialog.setTitle("Saved 1");
> > alertDialog.setMessage("Your drawing had been saved :)");
> > alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
> > public void onClick(DialogInterface dialog, int which) {
> > return;
> > }
> > });
> > alertDialog.show();
> > }
> > } ;
> > new ExportBitmapToFile(_panel, saveHandler, mBitmap).execute(null);
> > return true;
> > }
> >
> >
> > The ExportBitmapToFile class is as follows:
> > private class ExportBitmapToFile extends AsyncTask<Intent,Void,Boolean> {
> > private Context mContext;
> > private Handler mHandler;
> > private Bitmap nBitmap;
> > public ExportBitmapToFile(Context context,Handler handler,Bitmap bitmap)
> {
> > mContext = context;
> > nBitmap = bitmap;
> > mHandler = handler;
> > }
> > @Override
> > protected Boolean doInBackground(Intent... arg0) {
> > try {
> > if (!APP_FILE_PATH.exists()) {
> > APP_FILE_PATH.mkdirs();
> > }
> > final FileOutputStream out = new FileOutputStream(new File(APP_FILE_PATH
> +
> > "/myAwesomeDrawing.png"));
> > nBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
> > out.flush();
> > out.close();
> > return true;
> > }catch (Exception e) {
> > e.printStackTrace();
> > }
> > Toast.makeText(_panel, "Success!", 5);
> > return false;
> > }
> >
> > @Override
> > protected void onPostExecute(Boolean bool) {
> > super.onPostExecute(bool);
> > if ( bool ){
> > mHandler.sendEmptyMessage(1);
> > }
> > }
> > }
> > The logcat is attached.
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> > http://groups.google.com/group/android-developers?hl=en
>
>
>
> --
> Mark Murphy (a Commons Guy)
> http://commonsware.com | http://github.com/commonsguy
> http://commonsware.com/blog | http://twitter.com/commonsguy
>
> Android Training...At Your Office: http://commonsware.com/training
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>
--
--------------------
Raghav Sood
CEO/Founder/Owner/Dictator/Tyrant at Appaholics (Basically all titles
required to have complete control)
http://www.raghavsood.com/
https://market.android.com/developer?pub=Appaholics
http://www.appaholics.in/
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en