将位图转换为byte []&将byte []写入file:illegalArgument exception

时间:2017-02-08 06:06:51

标签: android bitmap io

 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); 坏基地64。是否有其他方法可以在不使用任何压缩的情况下完成此任务?

stacktrace --->

  

at dalvik.system.VMRuntime.newNonMovableArray(Native Method)       在android.graphics.BitmapFactory.nativeDecodeStream(Native Method)02-08   在android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)    I / dalvikvm:
  在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
  在android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391)
  在android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417)
  at nl.changer.socialschools.common.Utils.resizeImage(Utils.java:408)
  在nl.changer.socialschools.common.Utils.uploadPhotos(Utils.java:321)   at 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)

1 个答案:

答案 0 :(得分:0)

试试这个,

 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();
}
相关问题