как создать изображение в jsonobject - PullRequest
0 голосов
/ 25 июня 2019

Я хочу поместить изображение в jsonobject, поэтому преобразовал его в строку, используя (base64-encodetostring), и я не думаю, что из-за флага encodetostring base64 или idk, я пробовал несколько способов решения проблемы, ничего не помогает, и никаких решений вИнтернет.

Я пробовал флаг base64.DEFAULT, и любой отдельный FLAG ничем не помогает, потому что я думаю, что это потому, что base64.encodetostring не устанавливает конец строки или не знает точно, что это такое

                pdp.compress(Bitmap.CompressFormat.JPEG,100,bytes);

                byte[] bytes1=bytes.toByteArray();

                Pdp= Base64.encodeToString(bytes1,Base64.NO_WRAP|Base64.URL_SAFE);

и я поместил точную строку Pdp в jsono

                query.put("new_pdp",",pdp=cast('"+pdp+"==' as varbinary(max))");
            }else query.put("new_pdp","");

Я записал ее на консоль, и это результат только закодированных байтов:

_9j_4AAQSkZJRgABAQAAAQABAAD_4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAIQAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAFgAAAAcAEEAZABvAGIAZQBSAEcAQgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAACcGAAAT6UAAAT8WFlaIAAAAAAAADSNAACgLAAAD5VYWVogAAAAAAAAJjEAABAvAAC-nHBhcmEAAAAAAAQAAAACMzMAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABYWVogAAAAAAAA9tYAAQAAAADTLW1sdWMAAAAAAAAAAQAAAAxlblVTAAAAIAAAABwARwBvAG8AZwBsAGUAIABJAG4AYwAuACAAMgAwADEANv_bAEMAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf_bAEMBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf_AABEIBRQFFAMBIgACEQEDEQH_xAAfAAEAAAYDAQEAAAAAAAAAAAAABAUGCQoLAwcIAQL_xAB-EAABAgYAAwIJBQgLCAwHAh8BAAIDBAUGBxEIEiEJMQoTFBUWIkFRYTJxgZHwFyMkJjahsdElJzU3QkZSVsHh8RgzNEVVYmaCQ0RUZGVncoOWorLndHV2d4aSlbMZGihTY5SXpra3x0eFtdI4OVeTxddYaHN4hIejpcLT5v_EABoBAQEBAQEBAQAAAAAAAAAAAAAEBQEGAwL_xAA-EQACAQEGAwUFBwQCAgMBAQAAAREhAgQxQVFhcZHwEhSBobEFIsHR8QMyNEJisuEVJFKicoITkiNTwkRD_9oADAMBAAIRAxEAPwDP4REQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBFK5yqU6nj8OnpOU33iZm2s-sOI7_sFNEAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREARQ6iEAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBQsxHgwIXjo8bydvTbnEdO_pojr9XT4L5MRoEtCMePGEvLwB6xJ5W6AAG-hJ9wbon86pKHLGuEzNSlzBorWl0lT5kN1Pahhxq9ZZ6umAMHiobj0B5nknvA6jrU4JyaqUadbNTEw0Bs40SGg1rQAGjXcAAABoa9gXaVkNjQKC2WcIfkcvPlsmH9OYdBEG-8u8_iYAPTp07lI6PbFCqk1UY3iY8ajyw8hkpvyqJJDoG-MDRDdBe4NLmgRDyjqNs2QB2nLwYEtCECBBEvLwB6oA5W6AJOupJ95dsn8yAi0REAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBSmqz0aQptQnZeAZmZlZN022T36zixriW7HwY4nX8n4hRczGl5GAY8eKJeWgN9Yno3XsHtJHs1r4_FUZMUZtxPNRqomZaXldijSgLAZB7QC2tbDgTEIOwHb5Wg779gDqHx_wCGeUeUfhXl-_Lf8b67t-7f0_H4rvK0JiZnrepsxPxPKZkwtmc9Xc5rm--jfXTurSD09Tv0DqhKBa8nVIE1HnGzRlvKnyMg-TJpbZ-lt5OSuOaNPPjOZx2CNch2CXBdtS8GBLQhAgQRLy8AeqAOVugCTrqSfeXbJ_MgItERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBFCTEaBLQjHjxhLy8AesSeVugABvoSfcG6J_OqNdX6_PQTMU62XTEsNBhm6zDpbpwHoTDBhuAB0d87S36OiA5mObctUmxGI8zUOdfJGWcARPVSHrmiOPQFkLx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истрированный вывод jsonobject после помещения в него закодированных байтов:

{
"operation":"edit_profile",
"username":"frsyescd",
"password":"123Ch@rif",
"new_email":"",
"new_pdp":"pdp=cast('_9j_4AAQSkZJRgABAQAAAQABAAD_4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAIQAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAFgAAAAcAEEAZABvAGIAZQBSAEcAQgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAACcGAAAT6UAAAT8WFlaIAAAAAAAADSNAACgLAAAD5VYWVogAAAAAAAAJjEAABAvAAC-nHBhcmEAAAAAAAQAAAACMzMAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABYWVogAAAAAAAA9tYAAQAAAADTLW1sdWMAAAAAAAAAAQAAAAxlblVTAAAAIAAAABwARwBvAG8AZwBsAGUAIABJAG4AYwAuACAAMgAwADEANv_bAEMAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf_bAEMBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf_AABEIBRQFFAMBIgACEQEDEQH_xAAfAAEAAAYDAQEAAAAAAAAAAAAABAUGCQoLAwcIAQL_xAB-EAABAgYAAwIJBQgLCAwHAh8BAAIDBAUGBxEIEiEJMQoTFBUWIkFRYTJxgZHwFyMkJjahsdElJzU3QkZSVsHh8RgzNEVVYmaCQ0RUZGVncoOWorLndHV2d4aSlbMZGihTY5SXpra3x0eFtdI4OVeTxddYaHN4hIejpcLT5v_EABoBAQEBAQEBAQAAAAAAAAAAAAAEBQEGAwL_xAA-EQACAQEGAwUFBwQCAgMBAQAAAREhAgQxQVFhcZHwEhSBobEFIsHR8QMyNEJisuEVJFKicoITkiNTwkRD_9oADAMBAAIRAxEAPwDP4REQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBFK5yqU6nj8OnpOU33iZm2s-sOI7_sFNEAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREARQ6iEAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBQsxHgwIXjo8bydvTbnEdO_pojr9XT4L5MRoEtCMePGEvLwB6xJ5W6AAG-hJ9wbon86pKHLGuEzNSlzBorWl0lT5kN1Pahhxq9ZZ6umAMHiobj0B5nknvA6jrU4JyaqUadbNTEw0Bs40SGg1rQAGjXcAAABoa9gXaVkNjQKC2WcIfkcvPlsmH9OYdBEG-8u8_iYAPTp07lI6PbFCqk1UY3iY8ajyw8hkpvyqJJDoG-MDRDdBe4NLmgRDyjqNs2QB2nLwYEtCECBBEvLwB6oA5W6AJOupJ95dsn8yAi0REAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBSmqz0aQptQnZeAZmZlZN022T36zixriW7HwY4nX8n4hRczGl5GAY8eKJeWgN9Yno3XsHtJHs1r4_FUZMUZtxPNRqomZaXldijSgLAZB7QC2tbDgTEIOwHb5Wg779gDqHx_wCGeUeUfhXl-_Lf8b67t-7f0_H4rvK0JiZnrepsxPxPKZkwtmc9Xc5rm--jfXTurSD09Tv0DqhKBa8nVIE1HnGzRlvKnyMg-TJpbZ-lt5OSuOaNPPjOZx2CNch2CXBdtS8GBLQhAgQRLy8AeqAOVugCTrqSfeXbJ_MgItERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBFCTEaBLQjHjxhLy8AesSeVugABvoSfcG6J_OqNdX6_PQTMU62XTEsNBhm6zDpbpwHoTDBhuAB0d87S36OiA5mObctUmxGI8zUOdfJGWcARPVSHrmiOPQFkLxgDW76ucPe4j7PeNuKcmKRBEaWo0qXSlYmxyAVB3yIlGh707XLF1FiAnTYnKNl-nyGmVmSpcSpxnCZgU6YqDXVqSqDS-qUCrV-Ix4c4aZDZbpAHI7ryPJewFjXME_nbVkajMmfl5iYlBND9mBJANZXWBg5WxDzHuLSOZns00DptARIMapMdIUotpdOlmmTM9LBsMkBvTzK6GCwBmuUxCA0AAAO11iqJOx5p1Qk5qYl5mZp014t0eWGgecFwD2ddRG9WuHXvI9nXimJjx0Y0ikkS_koIm5tuvwAAEgAOOi4gHvOiPhsqdycnLSMHyeVgNgQG9Q0Hod9_TZI6e8_nQEYiIgCIiAIiIAiIgCIiAIiIAiIgCIiAIiIAiIgCIiAIiIAiIgCIiAIiIAiIgCIiAIiIAqdnrikJKaFPPlUzPlocZSRlYk9EAI3shobrQ79uaR12FC1OqRpma80UnTqgQfLJwFj20RjmN094IG3OG9AHrvZ3zACFPktFYKRRILZiqPBnXCbcTyl4ax1ZrkUCHEO3jvDRELgOVrTyhgHKbp8SXCPQa_LSwH-FmRJHvBIB_Od9N6B0p7IT9PqUHx8jMy000deeUcCPm3vfu66A30VOxajdsoJeNFpNNqUDR8sNKm4jXg6PrMbMNBcBrZ0CO_ehpS0SdqXHMPq0jPTVMqZBkZqJIT3mqqDuHi3gO5g7qCCByubvR2CgK5nZ2RkYJjT8zLSsD2vmphsNvf_AJ5Id7O4nSl0Cu0ediS8GDVaZMTEyHGTbJzrXl4DdkgMc7r07j1PQaVNy8vZVDmxLy8FtUqw7w0edaofn3s-7uI6d_Rc4gWxXI0tJTNJdTZotE9JsmpEUypBwOnOaOXn31cCOb2a5dgFAV6ioqlz0xTJqWoNWjTEyXcopNYiaJqI5dljywub4xrSN723WgSRylVqgCIiAIiIAiIgCIiAIiI"
}

Ответы [ 3 ]

1 голос
/ 26 июня 2019

Декодирование и кодирование Base64 может занимать очень много памяти. Если изображение находится на сервере, просто захватите URL и запросите его.

0 голосов
/ 28 июня 2019

, как упомянуто выше @Haider Malik, кодирование и декодирование base64 не требуют памяти, в качестве решения вы можете использовать firestore из firebase в качестве поставщика БД и загружать туда изображения. пример загрузки изображения будет выглядеть так:

    InputStream stream = new FileInputStream(new File("path/to/images/rivers.jpg"));

uploadTask = mountainsRef.putStream(stream);
uploadTask.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle unsuccessful uploads
    }
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
        // ...
    }
});StorageActivity.java

Официальная документация

0 голосов
/ 25 июня 2019

Вот как я это сделал:

     String photoEncodedString = ""; 

    if (this.theBitmap != null) {           
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        this.theBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] bytes = stream.toByteArray();
        photoEncodedString = Base64.encodeToString(bytes, Base64.DEFAULT);
    }

    String dataString = "[{";

    dataString = dataString + "\"PhotoImage\": \"" + photoEncodedString + "\" ";        
    dataString = dataString + ",\"Name\": \"" + etYourName.getText().toString() + "\" ";
    dataString = dataString + ",\"Email\": \"" + etEmail.getText().toString() + "\" ";
    dataString = dataString + ",\"Comments\": \"" + tvCommentsValue.getText().toString() + "\" ";

    dataString = dataString + "}]";

    JSONObject jObj = new JSONObject(dataString);

Я думаю, что ваша ошибка .put связана с неправильным форматированием вашего JSON.У вас есть

    { "operation":"edit_profile",
"username":"frsyescd",
"password":"123Ch@rif",
"new_email":"",
"new_pdp":",pdp=cast('_9j_4AAQSkZJRgABAQAAAQABAAD

Там, где перед pdp = cast стоит запятая.
Также "new_pdp": "pdp = cast (" также не будет действительным JSON.

IЯ просто говорю, что это недопустимый JSON, поэтому он может вызвать ошибку где-то:

 "new_pdp":",pdp=cast('_9j_4AAQSkZJRgABAQAAAQABAAD..._svmCveT8v

Вы не заканчиваете строку, основываясь на том, что вы опубликовали выше, вам нужно, чтобы она выглядела болеевот так, чтобы он был действительным JSON:

"new_pdp":"_9j_4AAQSkZJRgABAQAAAQABAAD..._svmCveT8v"

Вот снова мой код, но с нужным вам форматом JSON:

 String photoEncodedString = ""; 

if (pdp != null) {           
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    pdp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] bytes = stream.toByteArray();
    photoEncodedString = Base64.encodeToString(bytes, Base64.DEFAULT);
}

String dataString = "[{";

dataString = dataString + "\"operation\":\"edit_profile\" ";        
dataString = dataString + ",\"username\":\"frsyescd\" ";
dataString = dataString + ",\"password\":\"123Ch@rif\" ";
dataString = dataString + ",\"new_pdp\":\"" + photoEncodedString + "\""; 

dataString = dataString + "}]";

JSONObject jObj = new JSONObject(dataString);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...