如何修复java.io.NotSerializableException:android.graphics.Bitmap [英] How to fix a java.io.NotSerializableException: android.graphics.Bitmap

查看:1363
本文介绍了如何修复java.io.NotSerializableException:android.graphics.Bitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我尝试以节省筛网内部储存图像的位置,但我得到一个NotSerializableException.I去寻找,发现问题是位图的目的不是这个链接的Problem序列绘制对象我从来没有真正了解他是如何解决这个问题抛出的例子。如果有人可以解释他是如何解决他的NotSerializableException,并帮助我与我拿到这将是极大的AP preciated


下面是我的Elememt类

 公共类元素扩展的主要实现Serializable {
私人诠释MX;
私人诠释我;
INT LOCATION2;
矩阵elementlocation;
私人位图mBitmap;
帆布canvas2;

公共元素(资源RES,诠释的x,int y)对{
  LOCATION2 =项目3;
    mBitmap = BitmapFactory.de codeResource(RES,地点2);
    MX = X  -  mBitmap.getWidth()/ 2;
    我= Y  -  mBitmap.getHeight()/ 2;
  }

公共元素(){

}
公共无效doDraw2(帆布油画){
    elementlocation =新的Matrix();
    elementlocation.postTranslate(MX,MY);
    canvas2 =帆布;
    canvas2.drawBitmap(mBitmap,elementlocation,NULL);
    }
公共无效setelementlocation(浮动NUM1,NUM2浮动){
   elementlocation =新的Matrix();
   elementlocation.postTranslate(NUM1,NUM2);
 }
 公共帆布getCanvas2(){
    返程(canvas2);
 }
公共字符串的toString(){
    串句;
    一句话= mBitmap ++ MX ++我的;
    返回(句子);
 }

 }
 


下面是我onTouch方法在我的面板类

 公共布尔的onTouchEvent(MotionEvent事件){
   mainactivity =新的main();
    Log.v(测试,你已经触及了筛网:);

 mElements.add(新元素(getResources(),(int)的event.getX(),(int)的event.get()));
 mainactivity.writeElement(新元素(getResources(),(int)的event.getX(),(int)的event.getY()),this.getContext());
        Log.v(手势,是1);
        返回super.onTouchEvent(事件);
}
 


下面是我写元件的方法在我的主类

 公共无效writeElement(元件OBJ,上下文语境){
    Log.v(主,去到方法writeElement);
    文件F =新的文件(context.getFilesDir(),文件名);
    尝试 {
FOS =新的FileOutputStream(F);
    ObjectOutputStream的objectwrite =新的ObjectOutputStream(FOS);
    objectwrite.writeObject(OBJ);
 fos.close();
 Log.v(主,文件制作文件);

 }赶上(FileNotFoundException异常E){
    e.printStackTrace();
    Log.v(主,没有发文件未找到文件);
 }赶上(IOException异常E){
    // TODO自动生成的catch块
    e.printStackTrace();
    Log.v(主,没有发文件,文件IOException异常);
}
 }
 


更新

 公共元素(资源RES,诠释的x,int y)对{
    LOCATION2 =项目3;
    ByteArrayOutputStream流=新ByteArrayOutputStream();
    mBitmap = BitmapFactory.de codeResource(RES,地点2);
    MX = X  -  mBitmap.getWidth()/ 2;
    我= Y  -  mBitmap.getHeight()/ 2;
    mBitmap.com preSS(Bitmap.Com pressFormat.PNG,100,流);


}
 

解决方案

我只是碰到了同样的问题。 正如彼得指出,您使用的几个类是不可序列化。

Java的默认机制将尝试序列,你标记为可序列化类,并尽量坚持不标记为各个领域的瞬时。 如果这些字段中的一个然而是的序列化(如android.graphics.Bitmap),则该呼叫到ObjectOutputStream.writeObject将失败。

要解决这个问题,你需要重写的Java序列化如何你的元素类。 对于我CachedBitmap类,我用下面的code。

 包XXX;

进口java.io.IOException异常;
进口java.io.ObjectInputStream中;
进口java.io.ObjectOutputStream中;
的Bean;
进口java.util.Date;

进口org.apache.commons.io.output.ByteArrayOutputStream;

进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;

公共类CachedBitmap实现Serializable {

    私有静态最后长的serialVersionUID = -6298516694275121291L;

    插入日期;
    短暂的点阵位图;
    字符串URL;

    公共CachedBitmap(){};

    公共CachedBitmap(位图B,日期insertionDate,串sourceUrl){
        位= B;
        插入= insertionDate;
        URL = sourceUrl;
    }

    私人无效的writeObject(ObjectOutputStream的OOS)抛出IOException异常{
       //这将序列化,你没有用'瞬间'标记所有领域
       //(Java的默认行为)
        oos.defaultWriteObject();
       //现在,手动序列化要被序列化的所有瞬态领域
        如果(位图!= NULL){
            ByteArrayOutputStream字节流=新ByteArrayOutputStream();
            布尔成功= bitmap.com preSS(Bitmap.Com pressFormat.PNG,100字节流);
            如果(成功){
                oos.writeObject(byteStream.toByteArray());
            }
        }
    }

    私人无效的readObject(ObjectInputStream的OIS)抛出IOException异常,ClassNotFoundException异常{
       //现在,所有再次,反序列化 - 以相同的顺序!
       //所有非瞬态字段
        ois.defaultReadObject();
       //所有其​​他领域,你系列化
        byte []的图像=(字节[])ois.readObject();
        如果(图像= NULL和放大器;!&安培; image.length> 0){
            位= BitmapFactory.de codeByteArray(图像,0,image.length);
        }
    }

}
 

有关画布矩阵,你需要找到合适的(序列化)再presentations存储它们。例如,你可以使用 Matrix.getValues​​() Matrix.setValues​​(...)方法把它翻译成一个序列化格式。

希望这有助于你。你也可能希望通过甲骨文关于连续性咨询以下参考资料:基础和的高级

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); 


}

解决方案

I just ran into the same problem. As Peter pointed out, several Classes that you use are not Serializable.

The default mechanism of Java will try to serialize your class that you marked as Serializable and try to persist all fields not marked as transient. If one of these fields however is not Serializable (such as android.graphics.Bitmap), then the call to ObjectOutputStream.writeObject will fail.

To work around this, you will need to override how Java serializes your Element class. For my CachedBitmap class, I used the following code.

package XXX;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

import org.apache.commons.io.output.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class CachedBitmap implements Serializable{

    private static final long serialVersionUID = -6298516694275121291L;

    Date inserted;
    transient Bitmap bitmap;
    String url;

    public CachedBitmap(){};

    public CachedBitmap(Bitmap b, Date insertionDate, String sourceUrl){
        bitmap = b;
        inserted = insertionDate;
        url = sourceUrl;
    }

    private void writeObject(ObjectOutputStream oos) throws IOException{
       // This will serialize all fields that you did not mark with 'transient'
       // (Java's default behaviour)
        oos.defaultWriteObject();
       // Now, manually serialize all transient fields that you want to be serialized
        if(bitmap!=null){
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            boolean success = bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
            if(success){
                oos.writeObject(byteStream.toByteArray());
            }
        }
    }

    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException{
       // Now, all again, deserializing - in the SAME ORDER!
       // All non-transient fields
        ois.defaultReadObject();
       // All other fields that you serialized
        byte[] image = (byte[]) ois.readObject();
        if(image != null && image.length > 0){
            bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
        }
    }

}

For Canvasand Matrix you will need to find suitable (serializable) representations to store them in. For example, you could use the Matrix.getValues() and Matrix.setValues(...) method to translate it into a serializable format.

Hope this helps you. You might also to want to consult the following reference material by Oracle concerning Serialization: Basics and Advanced

这篇关于如何修复java.io.NotSerializableException:android.graphics.Bitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆