从字节数组转换为可绘制图像会导致图像损坏:Android

Nat*_*lie 4 java sockets arrays android image

我正在将图像转换为字节数组。通过套接字发送它,然后一旦它通过套接字,我需要将它转换回可绘制、png 或任何类型的图像,我可以将其用作图像按钮的背景。问题是,当我转换为字节数组或从数组转换为可绘制对象时,文件已损坏。

我从手机上最后安装的应用程序中获取原始图像,如下所示,然后将其保存到手机上的文件中,以便我可以检查此时图像文件是否已成功捕获(确实如此。没有任何损坏这一点):

final PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(intent.getData().getSchemeSpecificPart(), 0);
Drawable icon = context.getPackageManager().getApplicationIcon(ai);
BitmapDrawable bitmapIcon = (BitmapDrawable)icon;

FileOutputStream fosIcon = context.openFileOutput(applicationName + ".png", Context.MODE_PRIVATE);

bitmapIcon.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, fosIcon);
InputStream inputStream = context.openFileInput(applicationName + ".png");

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);


// GET FILE DIRECTORY

File imageFile = new File(context.getFilesDir(), applicationName + ".png");
Run Code Online (Sandbox Code Playgroud)

现在我将此位图转换为字节数组以通过套接字发送:

// get bitmap image in bytes to send

        int bytes = bitmap.getByteCount();
        Log.d("tag_name", "Number of Bytes" + bytes);

        ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
        bitmap.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

        byte[] array = buffer.array();
        Log.d("tag_name", "array" + array);
        int start=0;
        int len=array.length;
        Log.d("tag_name", "length" + len);


new SendToClient(array, applicationName, len, start).execute();
Run Code Online (Sandbox Code Playgroud)

此时我知道我的文件已成功保存为以下图像:

在此处输入图片说明

然后,在 SendToClient 中,我使用 DataOutputStream 发送数组。我没有发布此代码,因为我已经测试过发送数据不是问题发生的地方。如果我不发送字节数组,并在同一活动中从字节数组转换回可绘制对象,它也会损坏。

以下是我在使用 DataInputStream 读取发送的数组后从数组转换回可绘制对象的方法:

YuvImage yuvimage=new YuvImage(array, ImageFormat.NV21, 100, 100, null);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yuvimage.compressToJpeg(new Rect(0, 0, 100, 100), 80, baos);
        byte[] jdata = baos.toByteArray();

        // Convert to Bitmap
        Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
        Log.d("tag_name", "bmp" + bmp);

        // convert bitmap to drawable
        final Drawable d = new BitmapDrawable(context.getResources(), bmp);
Run Code Online (Sandbox Code Playgroud)

我首先压缩为 JPEG 的原因是因为如果我只使用 BitmapFactory.decodeByteArray,那么我会得到“bmp = null”,首先转换为 JPEG 是我找到的唯一解决方案,我可以在其中获得不为空的位图,但现在我的形象已损坏。这就是 Drawable d 的样子:

在此处输入图片说明

Sat*_*r J 5

试试下面的代码,

将位图转换为 ByteArray。

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.myImage);
ByteArrayOutputStream opstream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, opstream);
byte[] bytArray = opstream.toByteArray();
Run Code Online (Sandbox Code Playgroud)

将字节数组转换为位图。

Bitmap bitmap = BitmapFactory.decodeByteArray(bytArray, 0, bytArray.length);
ImageView img = (ImageView) findViewById(R.id.imgv1);
img.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)

这可能对您有所帮助。