为什么我从位图转换后无法显示我的byte []图像?

时间:2015-05-21 19:14:07

标签: java android bitmap bytearray scale

我尝试编写一个接受图片(Bitmap)并返回byte[] array的方法。最后,我尝试将此byte[] array写入文件夹,以便我可以看到差异,但我的byte[] array无法显示,此外,它没有按比例缩小!这是我的方法:

private byte[] changeSize(Bitmap image) {
        byte[] picture;
        int width = image.getWidth();
        int height = image.getHeight();
        int newHeight = 0, newWidth = 0;
            if (width > 250 || height > 250) {
                    if (width > height) { //landscape-mode
                        newHeight = 200;
                        newWidth = (newHeight * width) / height;
                    } else { //portrait-mode
                        newWidth = 200;
                        newHeight = (newWidth * height) / width;
                    }
                } else {
                    Toast.makeText(this, "Something wrong!", Toast.LENGTH_LONG).show();
                }
        Bitmap sizeChanged = Bitmap.createScaledBitmap(image, newWidth, newHeight, true);
        //Convert bitmap to a byte array
        int bytes = sizeChanged.getByteCount(); 
        ByteBuffer bb = ByteBuffer.allocate(bytes); 
        sizeChanged.copyPixelsFromBuffer(bb); 
        picture = bb.array();
        //Write to a hd
        picturePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        String fileName = edFile.getText().toString() + "_downscaled" + ".jpg";
        File file = new File(picturePath, fileName);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(picture);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
} 

我尝试了几个小时让我的byte[] array可见,但我可能根本就不这样做。任何帮助或提示向我展示我出轨的地方是非常感谢。

1 个答案:

答案 0 :(得分:0)

这对我有用

public static Bitmap byteArraytoBitmap(byte[] bytes) {
    return (BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
}

public static byte[] bitmaptoByteArray(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); //PNG format is lossless and will ignore the quality setting!
    byte[] byteArray = stream.toByteArray();
    return byteArray;
}

public static Bitmap bitmapFromFile(File file) {
    //returns null if could not decode
    return BitmapFactory.decodeFile(file.getPath());
}

public static boolean saveImage(Bitmap image, String filePath) {
    LogInfo(TAG, "Saving image to: " + filePath);
    File file = new File(filePath);
    File fileDirectory = new File(file.getParent());
    LogInfo(TAG, fileDirectory.getPath());

    if (!fileDirectory.exists()) {
        if (!fileDirectory.mkdirs()) {
            Log.e(TAG, "ERROR CREATING DIRECTORIES");
            return false;
        }
    }

    try {
        file.createNewFile();
        FileOutputStream fo = new FileOutputStream(file);
        fo.write(bitmaptoByteArray(image));
        fo.flush();
        fo.close();
        return true;
    }
    catch (Exception e) {
        e.printStackTrace();
        return false;
    }

}