- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 TouchPoint 类,它实现了 Serializable,因为它包含 Bitmap,所以我为该类编写了 writeObject 和 readObject:
private void writeObject(ObjectOutputStream oos) throws IOException {
long t1 = System.currentTimeMillis();
oos.defaultWriteObject();
if(_bmp!=null){
int bytes = _bmp.getWidth()*_bmp.getHeight()*4;
ByteBuffer buffer = ByteBuffer.allocate(bytes);
_bmp.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
oos.writeObject(array);
}
Log.v("PaintFX","Elapsed Time: "+(System.currentTimeMillis()-t1));
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException{
ois.defaultReadObject();
byte[] data = (byte[]) ois.readObject();
if(data != null && data.length > 0){
_bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
}
}
问题是我得到
SkImageDecoder::Factory returned null
那我该如何解决呢。我知道可能的解决方案是将 writeObject() 更改为
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
_bmp.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
oos.writeObject(byteStream.toByteArray);
但是这种方法慢了将近 10 倍以上。
更新发现实际问题是在
之后buffer.array();
所有byte[]数组元素都是:0
最佳答案
最后,我找到了一种方法,使其既能运行又能更快。我在使用这种方法时遇到了两个问题:
我是这样解决的
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
if(_bmp!=null){
int bytes = _bmp.getWidth()*_bmp.getHeight()*4;
ByteBuffer buffer = ByteBuffer.allocate(bytes);
_bmp.copyPixelsToBuffer(buffer);
byte[] array = new byte[bytes]; // looks like this is extraneous memory allocation
if (buffer.hasArray()) {
try{
array = buffer.array();
} catch (BufferUnderflowException e) {
e.printStackTrace();
}
}
String configName = _bmp.getConfig().name();
oos.writeObject(array);
oos.writeInt(_bmp.getWidth());
oos.writeInt(_bmp.getHeight());
oos.writeObject(configName);
} else {
oos.writeObject(null);
}
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException{
ois.defaultReadObject();
byte[] data = (byte[]) ois.readObject();
if (data != null) {
int w = ois.readInt();
int h = ois.readInt();
String configName = (String) ois.readObject();
Bitmap.Config configBmp = Bitmap.Config.valueOf(configName);
Bitmap bitmap_tmp = Bitmap.createBitmap(w, h, configBmp);
ByteBuffer buffer = ByteBuffer.wrap(data);
bitmap_tmp.copyPixelsFromBuffer(buffer);
_bmp = bitmap_tmp.copy(configBmp,true);
bitmap_tmp.recycle();
} else {
_bmp = null;
}
}
这对我来说已经足够快了——比 bmp.compress 方式快大约 15 倍。希望这有帮助:)
关于java - decodeByteArray 和 copyPixelsToBuffer 不工作。 SkImageDecoder::Factory 返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10770263/
我正在尝试使用以下代码行将图像加载到我的 ImageView 中: iv_flagged.setImageResource(R.drawable.not_flagged); 但是,当我运行这行代码时,
我正在尝试显示来自网络的图像..我正在使用 RemoteImageView 的点火库..这是代码.. public class images extends Activity{ public Stri
我在 Android 上编程并通过此调用从远程 URL 读取图像 BitmapFactory.decodeStream( ... 随机的,我得到错误: SkImageDecoder::Factory
我正在尝试将图像从 URL 加载到 ImageView 但出现错误:SkImageDecoder::Factory 返回空值。我该如何解决? 这是我的代码: private class LoadIma
我正在使用我的本地主机来获取图像并在 ImageView 中查看。出于某种原因,我收到了 Factory 返回的 null 错误。我已经多次查看代码,但我看不出有什么问题。任何帮助将不胜感激! Gal
我正在尝试将屏幕截图从桌面发送到 Android,并在 Android 上显示。据我所知,返回 null 可能是因为我没有 inputStream.read() 读取所有输入流,因此我做了一个 whi
我将我的 View imageview 传递到我创建的这个 imageloader 类中,但由于某种原因我收到此错误“SkImageDecoder::Factory returned null”,这似
这是一种解码图像的方法。调用时,它会抛出 SkImageDecoder::Factory 并返回 null。 private static Bitmap decodeFile(File f) {
我正在尝试从远程服务器下载图片,下载的图片数量是 30。我用来下载图片的代码如下。一些图片下载成功,一些图片没有下载并引发上述异常。可能是什么问题。 public static Bitmap load
这个问题在这里已经有了答案: 关闭10 年前。 代码: URL url = new URL(bitmapurl); HttpURLConnection connection = (HttpURLCo
我正在尝试从我的 Environment.getExternalStorageDirectory() 加载屏幕截图并尝试将其转换为位图 public void onPictureTaken(Stri
目标是将 Bitmap 转换为 byte [],在 Bundle 数据的 Activity 之间传递它,然后重新转换它在稍后阶段返回到 Bitmap 以在 Imageview 中显示。 问题是每当我尝
我使用以下代码从 Internet 下载图像并将其显示在 Android ImageView 中。 private class DownloadImage extends AsyncTask {
我有一个 Spring MVC Web 服务,它以字节数组的形式返回图像。输出为 json 格式。格式为png这是图像的代码 fragment 。 BufferedImage img = ImageI
我试图将来自 URL 的图像放入 ImageView 中,但我不断收到“SkImageDecoder::Factory returned null”错误。 这是我的代码: private class
我有一个 TouchPoint 类,它实现了 Serializable,因为它包含 Bitmap,所以我为该类编写了 writeObject 和 readObject: private void wr
我是一名优秀的程序员,十分优秀!