【问题标题】:converting bitmap to byte[] & writing byte[] to file : illegalArgument exception将位图转换为字节 [] 并将字节 [] 写入文件:非法参数异常
【发布时间】:2017-06-25 15:23:58
【问题描述】:
 public static void writeBitmapWithCompress(final String localFileName, Bitmap b){
        int bytes = b.getByteCount();

        ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
        b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
        byte[] array = buffer.array();
       // byte[] encodedString  = Base64.encode(array, Base64.DEFAULT);
        byte[] decodedString = Base64.decode(array, Base64.DEFAULT);
       // Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

        Timber.d(">> social localFileName"+ localFileName);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(localFileName);
            fileOutputStream.write(decodedString);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

在这一行遇到异常 --> byte[] decodedString = Base64.decode(array, Base64.DEFAULT); 坏的base64。还有其他方法可以在不使用任何压缩的情况下完成这项任务吗?

堆栈跟踪--->

在 dalvik.system.VMRuntime.newNonMovableArray(本机方法) 在 android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 02-08 在 android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635) 我/dalvikvm:
在 android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
在 android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391)
在 android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417)
在 nl.changer.socialschools.common.Utils.resizeImage(Utils.java:408)
在 nl.changer.socialschools.common.Utils.uploadPhotos(Utils.java:321) 在 nl.changer.socialschools.AsyncPostService.createPost(AsyncPostService.java:75) nl.changer.socialschools.AsyncPostService.onHandleIntent(AsyncPostService.java:50) 在 android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
在 android.os.Handler.dispatchMessage(Handler.java:110) 02-08 android.os.Looper.loop(Looper.java:193) 02-08 11:41:36.214 android.os.HandlerThread.run(HandlerThread.java:61)

【问题讨论】:

  • 显示完整的异常堆栈跟踪和消息。它通常解释了问题。
  • 你应该 encode 将字节转换为 base64 字符串。不像你现在尝试做的decode
  • 但是为什么要使用base64呢?您可以将字节缓冲区直接写入文件。
  • 如果我不解码,则无法在图库中看到图像。您还有其他选择吗?

标签: android bitmap io


【解决方案1】:

试试这个,

 public void writeBitmapWithCompress(final String localFileName, Bitmap b){

    byte[] image_bytes = getBytes(b);

    try {
        FileOutputStream out = new FileOutputStream(localFileName);
        out.write(image_bytes);
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}


// convert bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}

【讨论】:

  • 不想使用压缩。想直接写文件。
猜你喜欢
  • 2014-06-06
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多