【发布时间】:2019-12-20 00:13:56
【问题描述】:
我正在开发一个将图像发送到服务器的应用程序。我想将来自应用程序的照片保存在本地服务器端文件夹中。为此,我使用 SpriingBoot 进行 API 开发,在 Android 上,我使用 Volley 库通过 Json 提交请求。
我已经尝试将请求中传入的字符串转换为字节[],然后将其保存为 Image.io 格式文件,但无法保存图像。谁能帮我把图片保存到本地服务器目录?
代码安卓:
public class MainActivity extends AppCompatActivity {
public static final String REGISTER_URL = "http://192.168.1.8:8080/api/paciente";
public static final String KEY_IMAGE = "foto";
String foto = "null";
public static final String TAG = "LOG";
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button enviar = (Button) findViewById(R.id.enviar);
enviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerForms();
}
});
}
public void tirarFoto(View view) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
Bitmap img = (Bitmap) bundle.get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.JPEG, 100, stream);
foto = Base64.encodeToString(stream.toByteArray(), Base64.DEFAULT);
Toast toast = Toast.makeText(getApplicationContext(), "Foto anexada", Toast.LENGTH_LONG);
toast.show();
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
public void registerForms() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.contains("Erro")) {
//progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Erro ao enviar", Toast.LENGTH_LONG).show();
Log.i(TAG, "Lat: " + "Caiu aqui");
} else {
//progressDialog.dismiss();
Intent intent = new Intent(MainActivity.this, MainActivity.class);
Toast.makeText(MainActivity.this, "Enviado com sucesso!", Toast.LENGTH_LONG).show();
startActivity(intent);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Enviado com sucesso!", Toast.LENGTH_LONG).show();
Log.i(TAG, "String: " + foto);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError{
Map<String, String> map = new HashMap<String, String>();
map.put(KEY_IMAGE, foto);
Log.i(TAG, "Lat: " + map);
return map;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
代码 API:
服务:
@RequestMapping(value = "/paciente", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = {
MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE, "application/json" })
public @ResponseBody Paciente cadastraPaciente(@Valid Paciente paciente) throws Exception {
byte[] imgBytes = Base64.decodeBase64(foto.getBytes());
try{
FileOutputStream fos = new FileOutputStream("C:/Users/carlo/Downloads/SisCAF/images/myImage1.png");
fos.write(imgBytes);
FileDescriptor fd = fos.getFD();
fos.flush();
fd.sync();
fos.close();
}
return paciente;
}
和代码一样,返回image属性为null
【问题讨论】:
-
假设
BufferedImage被正确读取,请尝试使用relative提及您的path,如下所示:"./out.jpg",它有效吗? -
@Phill 问题是将 ByteArrayInputStream 转换为 ByteArrayInputStream 因为它返回一个空图像
-
你的意思是
bis是空的,对吧? -
菲尔,是的,是空的
-
好的,
byte[ ] encoded呢?
标签: java android rest spring-boot web