有没有人知道使用改造2将图像上传到服务器的方法2.大多数在线解决方案都使用改造1.
感谢 我试过这个:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *subjectName[1000];
int numOfSubj = 0;
int numOfUserAddedSubj = 0;
subjectName[numOfSubj] = "Math";
numOfSubj++;
subjectName[numOfSubj] = "Physics";
numOfSubj++;
subjectName[numOfSubj] = "English";
numOfSubj++;
//int k;
//for(k=0; k<1; k++)
//{
// add user
printf("Enter new subject name: ");
fgets(subjectName[numOfSubj], 50, stdin);
numOfUserAddedSubj++;
numOfSubj++;
//}
// add another user
/*printf("Enter new subject name: ");
fgets(subjectName[numOfSubj], 50, stdin);
numOfUserAddedSubj++;
numOfSubj++;*/
// display content of array
int i;
for(i=0; i < ((strlen(subjectName))+numOfUserAddedSubj); i++)
{
printf("%s\n", subjectName[i]);
}
return 0;
}
我收到了以下错误:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//GALLERY
if ((isGalleryCalled(requestCode) && resultCode == RESULT_OK)) {
Log.w(TAG, "onActivityResult > isGalleryCalled > data.getData(): " + data.getData());
Uri originalUri = data.getData();
if (originalUri != null) {
mImageUri = originalUri;
}
}
//CAPTURE
else if ((isCaptureCalled(requestCode)) && resultCode == RESULT_OK) {
Log.w(TAG, "onActivityResult > isCaptureCalled > data.getData(): " + data.getData());
Uri originalUri = data.getData();
if (originalUri != null) {
mImageUri = originalUri;
}
}
private void prepareImage(String mImageUri ) {
File file = new File(mImageUri );
mRequestBodyImage = RequestBody.create(MediaType.parse("multipart/form-data"), file);
isUploadImage = true;
Log.d(TAG, "isUploadImage: " + isUploadImage + " | \nuri: " + uri + " | \nfile getPath: " + file.getPath());
}//end sendReport
Call<DefaultResponse> call = RestClient.get().uploadImage(file);
....
@Multipart
@POST(Constant.API_UPLOADIMAGE)
Call<DefaultResponse> uploadImage(
//Upload File
@Part("myfile\"; filename=\"image.png\" ") RequestBody file
);
[UPDATE]
从@Anton Shkurenko的反馈更新代码
图像路径开始处理
UtilApiHelp.getPath:/storage/emulated/0/DCIM/Camera/20151210_124246.jpg
sendErrorReport - file: com.squareup.okhttp.RequestBody$3@2f83a2f9
stat failed: ENOENT (No such file or directory) : content:/media/external/images/media/2720
java.io.FileNotFoundException: content:/media/external/images/media/2720: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at okio.Okio.source(Okio.java:163)
at com.squareup.okhttp.RequestBody$3.writeTo(RequestBody.java:117)
at com.squareup.okhttp.MultipartBuilder$MultipartRequestBody.writeOrCountBytes(MultipartBuilder.java:277)
at com.squareup.okhttp.MultipartBuilder$MultipartRequestBody.writeTo(MultipartBuilder.java:297)
错误:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第3行第2列路径为STRING
private void prepareImage(String uri) {
File file = new File(uri);
mRequestBodyImage = RequestBody.create(MediaType.parse("multipart/form-data"), file);
isUploadImage = true;
Log.d(TAG, "prepareImage isUploadImage: " + isUploadImage + " | \nuri: " + uri + " | \nfile getPath: " + file.getPath());
sendErrorReport("", mRequestBodyImage);// test1
uploadImage(file.getPath());//test 2
}//end sendReport
//API Send Image test 1 - FAILED
private void sendErrorReport(String s, String msg, RequestBody file){
Log.e(TAG, "sendErrorReport - file: " + file.toString());
String user_id = Preferences.getInstance().getUserId();
Call<DefaultResponse> call = RestClient.get().sendErrorReport(s, file);
call.enqueue(new Callback<DefaultResponse>() {
@Override
public void onResponse(Response<DefaultResponse> response, Retrofit retrofit) {
Log.w("sendErrorLog > onResponse => ", String.valueOf(response.isSuccess()));
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
});
}
错误:由以下引起:java.lang.IllegalArgumentException:@Body参数不能与表单或多部分编码一起使用。方法APIService.sendErrorReport
的(参数#1)
使用了这个电话
//Upload test 2 -> not working
private void uploadImage(final String path) {
final RequestBody photo =
UtilApiHelp.getImageBodyBuilder(new HashMap<String, String>() {{
put("image", path);
}})
//.addFormDataPart("here_you_can_add_another_form_data", anotherFormData)
.build();
Call<DefaultResponse> call = RestClient.get().sendErrorReport(photo);
call.enqueue(new Callback<DefaultResponse>() {
@Override
public void onResponse(Response<DefaultResponse> response, Retrofit retrofit) {
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
});
}
答案 0 :(得分:2)
<强> [UPDATE] 强>
自从您更新了问题。你无法从给定的Uri中找到该文件。使用我的代码中的util函数getPath
来获取图像路径,希望这可以工作。
[旧解决方案]
我有这个有效的解决方案:
Util功能:
public static RequestBody getImageBody(Map<String, String> map) {
return getImageBodyBuilder(map).build();
}
public static MultipartBuilder getImageBodyBuilder(Map<String, String> map) {
MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM);
for (String key : map.keySet()) {
final String filePlace = map.get(key);
Log.d(TAG, "File place: " + filePlace);
final String[] args = filePlace.split("\\.");
final String fileExt = args[args.length - 1];
Log.d(TAG, "File extension: " + fileExt);
// Old solution, porting from one project to another
// It's about problems with jpeg
builder.addPart(Headers.of("Content-Disposition",
"form-data; name=\"" + key + "\"; filename=\"" + filePlace + "\""), RequestBody.create(
MediaType.parse("image/" + fileExt.toLowerCase().replace("jpg", "jpeg")),
new File(map.get(key))));
}
return builder;
}
public static String getPath(Context ctx, Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = ctx.getContentResolver().query(uri, projection, null, null, null);
if (cursor == null) return null;
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String s = cursor.getString(columnIndex);
cursor.close();
return s;
}
照片上传。
final RequestBody photo =
ImageUploadUtils.getImageBodyBuilder(new HashMap<String, String>() {{
put("photo", imagePathWhateverYouWant);
}})
.addFormDataPart("here_you_can_add_another_form_data", anotherFormData).build();
mApiService.attachPhoto("Token " + getToken(), photo);
ApiService.java(接口):
// I user here RxJava, but I think it's easy to use Calls here.
@POST("deliveries/photos") Observable<Map<String, String>> attachPhoto(
@Header("Authorization") String token,
@Body RequestBody multipartBody);