我正在尝试借助 retrofit2 库将图像文件发送到服务器。但有一个问题。顺便说一句,我用 MVP 模式编写代码。
我尝试以不同的方式将图像文件发送到服务器。其中之一是用于快速响应的 AndroidNetworking。但最后他们都没有工作。问题是“找不到文件”。
我从手机图库中挑选图像,然后在 Android Image Cropper 库的帮助下将该图像发送到裁剪过程。这是我导致异常的代码:
File file = new File(images.get(cursor).toString());
//pass it like this
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("image", file.getName(), requestFile);
mInteractor.onImageUploadRequest(
body,
SharedPreferencesManager.getStringData(mView.getContext(), Constants.TOKEN_KEY),
new ImageCallback()
);
问题是:
E/SharePostPresenter: file:/data/user/0/org.kitapp/cache/cropped4247134528851651259.jpg (No such file or directory)
最佳答案
似乎问题很明显:
path of image is not correct.
但是,如果您使用的是 Image Cropper 库,那么您应该拥有裁剪图像的 Uri
,因此请尝试这样的操作:File file = new File(uri.getPath());
请注意,getPath()
是从 Uri
访问路径的正确方法,而不是使用 toString()
关于java - 没有这样的文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57767429/