你好我試圖保存一個圖像位置屏幕上的內部存儲,但我得到一個NotSerializableException.I去搜索,發現問題是,該位圖不是旨在被鏈接到這個鏈接Problem serializing Drawable我從來沒有真正明白他是如何解決這個問題的例子。如果有人能解釋他是如何解決他的NotSerializableException,並幫助我與我得到它,將不勝感激如何解決一個java.io.NotSerializableException:android.graphics.Bitmap
這裏是我的Elememt類
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);
}
}
這裏是我的面板在我onTouch方法類
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);
}
這裏是我的主類
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 ");
}
}
更新我寫元件方法
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);
}
您的問題不僅是'位圖',還包括'畫布'和'矩陣'。這兩個類都不是'Serializable'。你究竟在做什麼? –
@PeterKnego即時通訊用戶可以觸摸屏幕並在屏幕上放置某種圖像,但也可以拖動他放置的所有圖像,這樣當他填滿屏幕時,他可以拖動屏幕並獲得更多空間。爲什麼我有這個矩陣。當他完成了他想要的東西之後,我喜歡他爲了挽救他所做的事情,所以我需要將畫布放在內部存儲空間中。是否回答你的問題?簡而言之即時試圖做一個遊戲的關卡編輯器 –