gpt4 book ai didi

java - 无法在服务器上查看上传的图像

转载 作者:行者123 更新时间:2023-12-02 12:20:47 26 4
gpt4 key购买 nike

当我尝试在服务器上上传图像时,它在我的设备上显示“图像上传成功”,但我无法在服务器上看到该图像。
我什至尝试通过将图像转换为 Base64 来上传图像。

这是我的代码。

            try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String responseStr = EntityUtils.toString(response.getEntity());
Log.i(TAG, "doFileUpload Response : " + responseStr);
handler.sendEmptyMessage(1);
} catch (Exception e) {
System.out.println("Error in http connection " + e.toString());
handler.sendEmptyMessage(0);
}
}
});
t.start();

}

public String convertBitmapToString(Bitmap bmp){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 90, stream); //compress to which format you want.
byte[] byte_arr = stream.toByteArray();
String imageStr = Base64.encodeToString(byte_arr, Base64.DEFAULT);
return imageStr;
}

但是在运行时它显示错误:

E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f9e668360

最佳答案

new Thread(new Runnable() {
public void run() {
final JSONObject jsonObject = uploadImage(strImagePath);
if (jsonObject != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "Response : " + jsonObject.toString());
}
});
}
}
}).start();



public JSONObject uploadImage(String profileFileUri) {
try {

RequestBody r = null;
RequestBody k = null;
String extension = null;
String ext = null;


if (profileFileUri != null) {
File sourceFile = new File(profileFileUri);

Log.d(TAG, "File...::::" + sourceFile + " : " + sourceFile.exists());

final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
final MediaType MEDIA_TYPE_JPG = MediaType.parse("image/jpg");
final MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");
final MediaType MEDIA_TYPE_GIF = MediaType.parse("image/gif");

if (profileFileUri.contains(".jpg")) {
k = RequestBody.create(MEDIA_TYPE_JPG, sourceFile);
ext = ".jpg";
} else if (profileFileUri.contains(".jpeg")) {
ext = ".jpeg";
k = RequestBody.create(MEDIA_TYPE_JPEG, sourceFile);
} else if (profileFileUri.contains(".png")) {
ext = ".png";
k = RequestBody.create(MEDIA_TYPE_PNG, sourceFile);
} else if (profileFileUri.contains(".gif")) {
ext = ".gif";
k = RequestBody.create(MEDIA_TYPE_GIF, sourceFile);
}
}

MultipartBody requestBody;

if (k != null) {


requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)


.addFormDataPart("first_name", strFname)
.addFormDataPart("last_name", strLname)
.addFormDataPart("email", strEmail)
.addFormDataPart("gender", strGender)
.addFormDataPart("mobile", strMobile)
.addFormDataPart("birth_date", strBirthdate)
.addFormDataPart("profile_pic", "IMG" + System.currentTimeMillis() + ext, k)
.addFormDataPart("access_token", UserInfo.getAccessToken())
.build();

} else {


requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)


.addFormDataPart("first_name", strFname)
.addFormDataPart("last_name", strLname)
.addFormDataPart("email", strEmail)
.addFormDataPart("gender", strGender)
.addFormDataPart("profile_pic", "")
.addFormDataPart("mobile", strMobile)
.addFormDataPart("birth_date", strBirthdate)
.addFormDataPart("access_token", UserInfo.getAccessToken())
.build();


}


Log.d(TAG, "uploadImage param : " + requestBody.toString());
okhttp3.Request request = new okhttp3.Request.Builder()
.url(Consts.URL.EDIT_PROFILE)
.post(requestBody)
.build();


OkHttpClient client = new OkHttpClient();
okhttp3.Response response = client.newCall(request).execute();
String res = response.body().string();

Log.d(TAG, "Response : " + res);
return new JSONObject(res);

} catch (UnknownHostException | UnsupportedEncodingException e) {


Error.displayNetworkErrorDialog(ProfileActivity.this);

Log.e(TAG, "Error: " + e.getLocalizedMessage());
e.printStackTrace();
} catch (Exception e) {

Error.displayNetworkErrorDialog(ProfileActivity.this);
Log.e(TAG, "Other Error: " + e.getLocalizedMessage());
e.printStackTrace();
}
return null;
}

请尝试此代码将图像上传到服务器......!我希望它对你有帮助......!

关于java - 无法在服务器上查看上传的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45809782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com