我遍布stackoverflow寻找解决方案,我发现了一些,但我似乎无法让它们正常工作。我目前从我的api获得响应,这是我在这里保存在位图中的图像:
我想要做的是在条形码结果下面的主要活动上显示这个。
BarcodeCaptureActivity.java
mCameraSource.takePicture(null, new CameraSource.PictureCallback(){
@Override
public void onPictureTaken (byte[] data){
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
String imgData = Base64.encodeToString(data, Base64.NO_WRAP);
JSONObject jsonObj = new JSONObject();
try{
jsonObj.put("barcode", finalBest.displayValue);
jsonObj.put("imgurl", imgData);
Context jsonSuc = getApplicationContext();
CharSequence jsonText = "Json successful";
int jsonDur = Toast.LENGTH_SHORT;
Toast jsonToast = Toast.makeText(jsonSuc, jsonText, jsonDur);
jsonToast.show();
}
catch (JSONException e){
Context context2 = getApplicationContext();
CharSequence text2 = "Bye toast!";
int duration2 = Toast.LENGTH_SHORT;
Toast toast2 = Toast.makeText(context2, text2, duration2);
toast2.show();
}
try {
Context connTe = getApplicationContext();
CharSequence connectText = "Connection function successful";
int connectDur = Toast.LENGTH_SHORT;
Toast connectToast = Toast.makeText(connTe, connectText, connectDur);
connectToast.show();
URL url = new URL("http://MYAPI");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
Log.i("JSON", jsonObj.toString());
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
//os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
os.writeBytes(jsonObj.toString());
os.flush();
os.close();
//new
String json_response = "";
InputStreamReader in = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(in);
String resptext = "";
while ((resptext = br.readLine()) != null) {
json_response += resptext;
}
json_response = json_response.substring(15);
json_response = json_response.substring(0, json_response.length()-2);
try {
URL url2 = new URL(json_response);
HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
myBitmap = BitmapFactory.decodeStream(input); //HEre myBitmap contains an image
}catch (Exception e){
Log.d(TAG,e.getMessage());
}
Log.i("STATUS", String.valueOf(conn.getResponseCode()));
Log.i("MSG", conn.getResponseMessage());
conn.disconnect();
}
catch (Exception a) {
a.printStackTrace();
}
// Do something with the data (this is the JPEG image)
}
}
);
Intent data = new Intent();
data.putExtra(BarcodeObject, best);
data.putExtra("mybitmap", myBitmap); // Here myBitmap is empty
setResult(CommonStatusCodes.SUCCESS, data);
finish();
return true;
}
return false;
如何在主活动中访问此变量,以便在布局上绘制位图?