0

Service give Json Data

result = {"data":[{"image":"System.Byte[]"},{"image":"System.Byte[]"}]}

My Code

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
}

What can I do?

  • put full json here, and what ever you have shown is wrong byte data in json. thats why you are having null. – vikas kumar Sep 06 '17 at 04:02
  • 2
    Don't use JSON to store files. You got the string representation of the object `byte[]` and not the image's data. The problem is with your service. Consider giving the URL to the image instead of its data – Vanna Sep 06 '17 at 04:05
  • 1
    You can use base64 instead of "System.Byte[]". "System.Byte[]" just a string not byte data. –  Sep 06 '17 at 04:20

2 Answers2

1
// 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);

    }
jessica
  • 1,700
  • 1
  • 11
  • 17
0

try one of following:

 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);
Bapusaheb Shinde
  • 839
  • 2
  • 13
  • 16