Hello im trying to save a image location on sreen to internal storage but I get a NotSerializableException.I went searching and found that the problem is that Bitmap is not designed to be serialized at this link Problem serializing Drawable I never really understood how he fix the problem throw the example. if someone could explain how he fix his NotSerializableException and help me get with mine it would be greatly appreciated
Here is my Elememt Class
public class Element extends main implements Serializable{
private int mX;
private int mY;
int location2 ;
Matrix elementlocation;
private Bitmap mBitmap;
Canvas canvas2;
public Element(Resources res, int x, int y) {
location2 =item3;
mBitmap = BitmapFactory.decodeResource(res, location2);
mX = x - mBitmap.getWidth() / 2;
mY = y - mBitmap.getHeight() / 2;
}
public Element(){
}
public void doDraw2(Canvas canvas) {
elementlocation=new Matrix();
elementlocation.postTranslate(mX,mY);
canvas2=canvas;
canvas2.drawBitmap(mBitmap, elementlocation,null);
}
public void setelementlocation(float num1,float num2){
elementlocation=new Matrix();
elementlocation.postTranslate(num1,num2);
}
public Canvas getCanvas2(){
return(canvas2);
}
public String toString() {
String sentence;
sentence= mBitmap+" "+mX+" "+mY;
return (sentence);
}
}
Here is my onTouch method in my Panel class
public boolean onTouchEvent(MotionEvent event) {
mainactivity=new main();
Log.v("test", "you have touched the sreen: ");
mElements.add(new Element(getResources(),(int) event.getX(),(int) event.get()));
mainactivity.writeElement(new Element(getResources(),(int) event.getX(),(int) event.getY()),this.getContext());
Log.v("Gesture", "is 1 ");
return super.onTouchEvent(event);
}
Here is my write element method in my main class
public void writeElement(Element obj, Context context){
Log.v("main", "made it to method writeElement" );
File f = new File(context.getFilesDir(),FILENAME);
try {
fos = new FileOutputStream(f);
ObjectOutputStream objectwrite = new ObjectOutputStream(fos);
objectwrite.writeObject(obj);
fos.close();
Log.v("main", "file was made File ");
}catch (FileNotFoundException e){
e.printStackTrace();
Log.v("main", "file was not made File not found ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v("main", "file was not made File IOException ");
}
}
Update
public Element(Resources res, int x, int y) {
location2 =item3;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap = BitmapFactory.decodeResource(res, location2);
mX = x - mBitmap.getWidth() / 2;
mY = y - mBitmap.getHeight() / 2;
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
}
Bitmap
, but alsoCanvas
andMatrix
. Neither of those classes isSerializable
. What are you actually trying to do? – Thorley