当我尝试在服务器上传图像时,它在我的设备上显示“图像上传成功”,但我无法在服务器上看到图像。
我甚至尝试通过将图像转换为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
答案 0 :(得分:1)
使用以下代码从网址尝试Glide Library加载图片。
String url = myUrls.get(position);
GlideApp
.with(myFragment)
.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.into(myImageView);
答案 1 :(得分:0)
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;
}
请尝试使用此代码将图像上传到服务器.... 我希望它对你有帮助......!