我在通过相机上传文件时遇到问题。我的代码如下:
inputStreamImg = null;
if (requestCode == PICK_IMAGE_CAMERA) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Log.e("Activity", "Pick from Camera::>>> ");
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
destination = new File(Environment.getExternalStorageDirectory() + "/" +
getString(R.string.app_name), "IMG_" + timeStamp + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
path = destination.getAbsolutePath();
}
通过以下代码完成上传:
private void uploadToServer(String extension, String path, String Description) {
interface = ApiClient.getApiClient().create(Interface.class);
File file = new File(path);
RequestBody mFile = RequestBody.create(MediaType.parse("image/" + extension), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), mFile);
RequestBody fileName = RequestBody.create(MediaType.parse("text/plain"), file.getName());
RequestBody fileDescription = RequestBody.create(MediaType.parse("text/plain"), Description);
Call<UploadModel> fileUpload = kycInterface.uploadImage(fileToUpload, fileName, fileDescription);
}
尽管我拍摄的图像存储在手机的内存中,但我没有得到这样的目录作为错误。我不知道我的代码有什么问题。有人可以帮我吗?我需要上传从相机拍摄的照片。