我在做什么
我需要通过HTTPS请求发送一个JsonArray,其中包含Base64编码字符串中的一些数据和图像.如果数据存储在内存中,这很有效.
现在,我需要避免加载内存中的所有数据,我正在Android设备中创建一个临时文件,其中包含我需要发送的所有数据.为了创建文件,我在他里面写了很多JsonObjects.一些JsonObjects有一个表示图像的字段.当我检测到一个时,我得到了图像路径,并使用Base64作为字符串对其进行编码.
更新:
首先,我将文件理论化,然后获得bufferedWriter
File f = new File(privateSincronizePath+File.separator+"upload_"+timefile+".json");
f.createNewFile();
FileWriter fw = new FileWriter(f.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
Run Code Online (Sandbox Code Playgroud)
以下是存在时创建图像的代码:
JSONObject jobInf = new JSONObject();
jobInf.put("xx", "INSERT");
jobInf.put("xx", c.getString(c.getColumnIndex("xx")));
jobInf.put("xx", ""+c.getInt(c.getColumnIndex("xx")));
jobInf.put("xx", c.getString(c.getColumnIndex("xx")));
JSONObject jsonObject = new JSONObject(docu.filtre(dades, docu.getXx()));
Iterator<?> keys = jsonObject.keys();
boolean updated = false;
while(keys.hasNext() && !updated){
String key = (String)keys.next();
if (key != null && key.equals("path") && key.length() > 0){
jsonObject.put(key, getBase64EncodeFromImage(jsonObject.getString(key)));
updated = true;
}
}
jobInf.put("object", jsonObject);
escriure(bw, ","+jobInf.toString());
Run Code Online (Sandbox Code Playgroud)
方法escriure():
更新: …