怎样的android保存位图 - 马车code [英] android how to save a bitmap - buggy code

查看:123
本文介绍了怎样的android保存位图 - 马车code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着我有一个位图变量序列化类至极。这里是code,它是位工作....我需要帮助找出什么仍然是错的......

Im trying to serialize a class in wich i have a bitmap variable. Here is the code that is a bit working.... I need help to find out what is still wrong.....

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标志是适合......怎么知道?

Im not getting an error but the bitmap Im getting are wrong... also, I do not know how to know which Bitmap.Config flag is suitable... how to know ?

任何帮助吗?

推荐答案

下面是code与内存优化序列化。即时通讯使用一个静态缓冲区增长到最大的位图大小,并且我重用各一次。

Here is the code for a serialization with memory optimisation. Im using a static buffer that is growing to the biggest bitmap size and that I reuse each time.

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

}

这篇关于怎样的android保存位图 - 马车code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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