Java Android:从服务将字节转换为位图数据为空

Java Android:从服务将字节转换为位图数据为空,java,android,Java,Android,服务提供Json数据 result = {"data":[{"image":"System.Byte[]"},{"image":"System.Byte[]"}]} 我的代码 JSONArray datas = new JSONArray(result.getString("data")); for (int i=0;i<datas.length();i++){ JSONObject obj = items.getJSONObject(i); byte[] blob=ob

服务提供Json数据

result = {"data":[{"image":"System.Byte[]"},{"image":"System.Byte[]"}]}
我的代码

JSONArray datas = new JSONArray(result.getString("data"));
for (int i=0;i<datas.length();i++){
    JSONObject obj = items.getJSONObject(i);
    byte[] blob=obj.getString("image").getBytes();
    Bitmap bmp= BitmapFactory.decodeByteArray(blob,0,blob.length); // bmp is null
}
JSONArray data=newjsonarray(result.getString(“data”);
对于(int i=0;i请尝试以下操作之一:

 byte[] blob=obj.getString("image").getBytes();
 byte[] blob=obj.getString("image").getBytes(Charset.forName("UTF-8"));
 byte[] blob=obj.getString("image").getBytes(StandardCharsets.UTF_8);

将完整的json放在这里,在json中显示的是错误的字节数据。这就是为什么使用null。不要使用json存储文件。您得到了对象
byte[]的字符串表示形式
而不是图像的数据。问题在于您的服务。请考虑提供图像的URL而不是其数据。您可以使用base64而不是“System.Byte[]”。“System.Byte[]”只是一个字符串,而不是字节数据。
// use this Code, this will work, if the Image string from server contain Base64 string
public void setImage(String theBase64String) {

        String myTempFolderPath = CommonUtils.SDCardBasePath;
        // If the Image String is Base 64 String
        if (theBase64String != null) {
            byte[] image_data_bytes = Base64.decode(theBase64String, Base64.NO_WRAP);
            String Image_Data_str = null;
            try {
                Image_Data_str = new String(image_data_bytes, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String imageFileExt = ".jpg";
            if (Image_Data_str != null && Image_Data_str.contains("png")) {
                imageFileExt = ".png";
            }


            // Create File Name with Current Date Time
            long msTime = System.currentTimeMillis();
            Date dt = new Date(msTime);
            String format = "yyMMddHHmmssSSS";
            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
            String captchaFileName = String.format("Captcha" + "_%s", sdf.format(dt).toString());
            //String captchaFileName = "Captcha";
            captchaFileName = captchaFileName + imageFileExt;

            String imageSaveFolderPath = myTempFolderPath + "/" + captchaFileName;

            String imgPath = writeImageBytesOnSdCard(MainActivity.this, imageSaveFolderPath, Image_Data_str);

            try {
                Bitmap bitmapSquare = createFixSizeBitmap(imgPath, 600, 600);
                // Set image to ImagevIew
                myImageView.setImageURI(Uri.parse(imgPath));

                // Create Bitmap Image From Path.

            } catch (Exception e) {
            }
        }
    }
    public Bitmap createFixSizeBitmap(String theTargetFile, int theReqHight, int theReqWidth) {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(theTargetFile, bmOptions);
        bitmap = Bitmap.createScaledBitmap(bitmap, theReqWidth, theReqHight, true);
        return bitmap;
    }
    public String writeImageBytesOnSdCard(Activity theActivity, String theCustomImagePath, String theCustomImageData) {
        String imagePath = theCustomImagePath;
        String temp[] = theCustomImageData.split("\\,");
        if (temp[0].contains("data:image/png")) {
            imagePath = CommonUtils.getImagePath(imagePath, "png");
        } else {
            imagePath = CommonUtils.getImagePath(imagePath, "jpg");
        }
        byte[] data = null;
        try {
            //data=myImageData1.getBytes("UTF-8");
            data = Base64.decode(temp[1], Base64.DEFAULT);
            FileOutputStream outputStream = null;
            outputStream = new FileOutputStream(new File(imagePath), false);
            InputStream in = new ByteArrayInputStream(data);

            OutputStream out = new BufferedOutputStream(outputStream);
            byte[] loginBuffer = new byte[1024];
            int byteRead = 0;
            while ((byteRead = in.read(loginBuffer)) != -1) {
                out.write(loginBuffer, 0, byteRead);
            }
            out.close();
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // code for sending a notification to the media scanner
            updateMedia(theActivity, imagePath);

        } catch (FileNotFoundException e) {
            imagePath = "";
            e.printStackTrace();
        } catch (IOException e) {
            imagePath = "";
            e.printStackTrace();
        }
        return imagePath;
    }

    public void updateMedia(final Activity theActivity, String filepath) {
        MediaScannerConnection.scanFile(theActivity.getApplicationContext(), new String[]{filepath}, null, null);

    }