我已经开始研究我的第一个Android应用程序,并具有处理具有多个图层的图像的应用程序的基础.我能够将项目文件的平面版本导出为PNG,但我希望能够保存分层图像以供以后编辑(包括应用于某些图层的任何选项,例如基于文本的图层).
无论如何,我已经确保需要写入文件的类是'Serializable'但是由于android.graphics.Bitmap不可序列化而导致了一些路障.下面的代码实际上将Bitmap作为PNG输出到ByteArray中,并应作为'readObject'的一部分将其读回.但是,当代码运行时 - 我可以验证读入的'imageByteArrayLength'变量与输出的变量相同 - 但'Bitmap image'始终为null.
任何帮助将不胜感激.谢谢阅读.
private String title;
private int width;
private int height;
private Bitmap sourceImage;
private Canvas sourceCanvas;
private Bitmap currentImage;
private Canvas currentCanvas;
private Paint currentPaint;
private void writeObject(ObjectOutputStream out) throws IOException{
out.writeObject(title);
out.writeInt(width);
out.writeInt(height);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
currentImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageByteArray = stream.toByteArray();
int length = imageByteArray.length;
out.writeInt(length);
out.write(imageByteArray);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
this.title = (String)in.readObject();
this.width = in.readInt();
this.height = in.readInt();
int imageByteArrayLength = in.readInt(); …Run Code Online (Sandbox Code Playgroud) 我有一个生成Bitmap的pojo方法.完成后,我想将Bitmap抛出到UI活动.这是一个示例,但它是:
private void sendBitmap(Bitmap bitmap) {
// TODO - I'm in the pojo where I want to send the bitmap
}
Run Code Online (Sandbox Code Playgroud)
仅供参考:我想要通知的UI活动不在我的项目中.我的意思是,我的项目是一个SDK,这样另一个开发人员就会在它可用时抓住这些信息.
我一直试图解决这个问题,但有些困惑.
我在MyActivity中创建了一个回调接口.到现在为止还挺好.这是它到目前为止的样子:
import android.graphics.Bitmap;
public class MyActivity {
// protected void whateverOtherMethods (Bundle savedInstanceState)
// {
// .
// .
// .
// .
// .
// .
//
// }
/**
* Callback interface used to supply a bitmap.
*/
public interface MyCallback {
public void onBitmapReady(Bitmap bitmap);
}
}
Run Code Online (Sandbox Code Playgroud)
演示应用程序的DoSomethingActivity可以实现我创建的回调接口.这是演示应用程序的DoSomethingActivity中回调的实现:
private final MyCallback …Run Code Online (Sandbox Code Playgroud)