android如何保存位图-错误代码

android如何保存位图-错误代码,android,serialization,bitmap,save,Android,Serialization,Bitmap,Save,我试图序列化一个类,我有一个位图变量。这是一段运行良好的代码。。。。我需要帮助来找出仍然错误的地方 private Bitmap myVideoScreenshotBm; private void writeObject(ObjectOutputStream out) throws IOException{ out.writeInt(myVideoScreenshotBm.getRowBytes()); out.writeInt(myVideoScreenshotBm.get

我试图序列化一个类,我有一个位图变量。这是一段运行良好的代码。。。。我需要帮助来找出仍然错误的地方

private Bitmap myVideoScreenshotBm;

private void writeObject(ObjectOutputStream out) throws IOException{

    out.writeInt(myVideoScreenshotBm.getRowBytes());
    out.writeInt(myVideoScreenshotBm.getHeight());
    out.writeInt(myVideoScreenshotBm.getWidth());

    int bmSize = myVideoScreenshotBm.getHeight() * myVideoScreenshotBm.getRowBytes();
    ByteBuffer dst= ByteBuffer.allocate(bmSize);

    myVideoScreenshotBm.copyPixelsToBuffer(dst);

    byte[] bytesar=new byte[bmSize];
    dst.position(0);
    dst.get(bytesar);

    out.write(bytesar);


}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{

    int nbRowBytes=in.readInt();
    int height=in.readInt();
    int width=in.readInt();
    //
    int bmSize = nbRowBytes * height;
    byte[] toread= new byte[bmSize];

    in.read(toread, 0, toread.length);
    ByteBuffer dst= ByteBuffer.allocate(bmSize);
    dst.put(toread);
    dst.position(0);
    myVideoScreenshotBm=Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
    myVideoScreenshotBm.copyPixelsFromBuffer(dst);

}
我没有得到一个错误,但我得到的位图是错误的。。。另外,我不知道如何知道哪个Bitmap.Config标志是合适的。。。怎么知道


有什么帮助吗?

这是使用内存优化进行序列化的代码。我正在使用一个静态缓冲区,这个缓冲区正在增长到最大的位图大小,我每次都会重复使用它

public class Video implements Serializable{
public long videoId;
public String title;
public String publisher;
public String language;
public Date lastModified;
public Date published;
public String imageUrl;
public String url;
public Bitmap myVideoScreenshotBm;
public Date expireTime;
//public Drawable myVideoScreenshotDrawable;

private static ByteBuffer dst;
private static byte[] bytesar;

public Video (long newVideoId) {
    this.videoId=newVideoId;
}
private void writeObject(ObjectOutputStream out) throws IOException{

    out.writeLong(videoId);

    out.writeObject(title);
    out.writeObject(publisher);
    out.writeObject(language);
    out.writeObject(lastModified);
    out.writeObject(published);
    out.writeObject(expireTime);

    out.writeObject(imageUrl);
    out.writeObject(url);


    out.writeInt(myVideoScreenshotBm.getRowBytes());
    out.writeInt(myVideoScreenshotBm.getHeight());
    out.writeInt(myVideoScreenshotBm.getWidth());

    int bmSize = myVideoScreenshotBm.getRowBytes() * myVideoScreenshotBm.getHeight();
    if(dst==null || bmSize > dst.capacity())
        dst= ByteBuffer.allocate(bmSize);

    out.writeInt(dst.capacity());

    dst.position(0);

    myVideoScreenshotBm.copyPixelsToBuffer(dst);
    if(bytesar==null || bmSize > bytesar.length)
        bytesar=new byte[bmSize];

    dst.position(0);
    dst.get(bytesar);


    out.write(bytesar, 0, bytesar.length);

}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{

    videoId=in.readLong();

    title=(String) in.readObject();
    publisher=(String) in.readObject();
    language=(String) in.readObject();
    lastModified=(Date) in.readObject();
    published=(Date) in.readObject();
    expireTime=(Date) in.readObject();

    imageUrl = (String) in.readObject();
    url = (String) in.readObject();


    int nbRowBytes=in.readInt();
    int height=in.readInt();
    int width=in.readInt();

    int bmSize=in.readInt();



    if(bytesar==null || bmSize > bytesar.length)
        bytesar= new byte[bmSize];


    int offset=0;

    while(in.available()>0){
        offset=offset + in.read(bytesar, offset, in.available());
    }


    if(dst==null || bmSize > dst.capacity())
        dst= ByteBuffer.allocate(bmSize);
    dst.position(0);
    dst.put(bytesar);
    dst.position(0);
    myVideoScreenshotBm=Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    myVideoScreenshotBm.copyPixelsFromBuffer(dst);
    //in.close();
}

}

无需使用冗余阵列+所有逻辑+使用给定的方法操作缓冲区+utf更适合字符串并避免强制转换+同步(但它无论如何都不是线程安全的):


为了简化操作,可以对位图以外的所有字段使用标准序列化。只需将位图标记为瞬态,并使用out.defaultWriteObject();和.defaultReadObject();。这确实清理了代码:

private String title;
private String description;
private transient Bitmap icon;

private synchronized void writeObject(final ObjectOutputStream out)
    throws IOException {
  // Serialize everything but the image
  out.defaultWriteObject();

  // Now serialize the image
  out.writeInt(icon.getRowBytes());
  out.writeInt(icon.getHeight());
  out.writeInt(icon.getWidth());
  out.writeInt(icon.getConfig().ordinal());

  final int bmSize = icon.getRowBytes() * icon.getHeight();
  if (dst == null || bmSize > dst.capacity()) {
    dst = ByteBuffer.allocate(bmSize);
  }
  dst.rewind();
  icon.copyPixelsToBuffer(dst);
  dst.flip();
  out.write(dst.array(), 0, bmSize);
}

private void readObject(ObjectInputStream in) throws IOException,
    ClassNotFoundException {
  // Read everything but the image
  in.defaultReadObject();

  // Now read the image
  final int nbRowBytes = in.readInt();
  final int height = in.readInt();
  final int width = in.readInt();
  final Bitmap.Config config = Bitmap.Config.values()[in.readInt()];

  final int bmSize = nbRowBytes * height;
  if (dst == null || bmSize > dst.capacity()) {
    dst = ByteBuffer.allocate(bmSize);
  }
  dst.rewind();
  in.read(dst.array(), 0, bmSize);

  icon = Bitmap.createBitmap(width, height, config);
  icon.copyPixelsFromBuffer(dst);
}

我是Java/Android世界的新手,我已经为我的类实现了您的示例代码。它还具有一些基本类型属性和位图类型属性。然而,在readObject方法中,当我尝试读取第一个属性时,我得到了一个OptionalDataException,即.readObject()中的“imageSource=(String)”;“我不知道为什么会发生这种情况,任何想法都将不胜感激。谢谢…我只看到两个可能的原因。首先,异常本身告诉您:“表示ObjectInputStream类遇到了一个基元类型(int、char等),而不是输入流中的对象实例。”。可能是您读取数据的顺序与在序列化对象中写入数据的顺序不同。小心遵守秩序。。。第一次写,第一次读。这对我来说很好,除了我在这里遇到的问题-。有什么想法吗?这对我来说很好,除了我在这里遇到的问题-stackoverflow.com/questions/10442761/…。有什么想法吗?
private String title;
private String description;
private transient Bitmap icon;

private synchronized void writeObject(final ObjectOutputStream out)
    throws IOException {
  // Serialize everything but the image
  out.defaultWriteObject();

  // Now serialize the image
  out.writeInt(icon.getRowBytes());
  out.writeInt(icon.getHeight());
  out.writeInt(icon.getWidth());
  out.writeInt(icon.getConfig().ordinal());

  final int bmSize = icon.getRowBytes() * icon.getHeight();
  if (dst == null || bmSize > dst.capacity()) {
    dst = ByteBuffer.allocate(bmSize);
  }
  dst.rewind();
  icon.copyPixelsToBuffer(dst);
  dst.flip();
  out.write(dst.array(), 0, bmSize);
}

private void readObject(ObjectInputStream in) throws IOException,
    ClassNotFoundException {
  // Read everything but the image
  in.defaultReadObject();

  // Now read the image
  final int nbRowBytes = in.readInt();
  final int height = in.readInt();
  final int width = in.readInt();
  final Bitmap.Config config = Bitmap.Config.values()[in.readInt()];

  final int bmSize = nbRowBytes * height;
  if (dst == null || bmSize > dst.capacity()) {
    dst = ByteBuffer.allocate(bmSize);
  }
  dst.rewind();
  in.read(dst.array(), 0, bmSize);

  icon = Bitmap.createBitmap(width, height, config);
  icon.copyPixelsFromBuffer(dst);
}