3 回答
TA贡献1851条经验 获得超4个赞
public void uploadimage() {
String filePath1 = getRealPathFromURIPath(uri1, AddVehicleFromNavBar.this);
String filePath2 = getRealPathFromURIPath(uri2, AddVehicleFromNavBar.this);
/* Log.d("hanish123456", "File path-> " + filePath1);
Log.d("hanish123456", "File path-> " + filePath2);*/
file1 = new File(filePath1);
file2 = new File(filePath2);
/*Log.d("hanish12345", "Filename " + imgname1);
Log.d("hanish12345", "Filename " + imgname2);*/
Bitmap bmp1 = BitmapFactory.decodeFile(file1.getAbsolutePath());
ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
bmp1.compress(Bitmap.CompressFormat.JPEG, 30, bos1);
Bitmap bmp2 = BitmapFactory.decodeFile(file2.getAbsolutePath());
ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
bmp2.compress(Bitmap.CompressFormat.JPEG, 30, bos2);
MultipartBody.Part fileToUpload1 = MultipartBody.Part.createFormData("image", imgname1, RequestBody.create(MediaType.parse("image/*"), bos1.toByteArray()));
RequestBody filename1 = RequestBody.create(MediaType.parse("text/plain"), imgname1);
MultipartBody.Part fileToUpload2 = MultipartBody.Part.createFormData("image", imgname2, RequestBody.create(MediaType.parse("image/*"), bos2.toByteArray()));
RequestBody filename2 = RequestBody.create(MediaType.parse("text/plain"), imgname2);
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(3, TimeUnit.MINUTES)
.readTimeout(3, TimeUnit.MINUTES)
.writeTimeout(3, TimeUnit.MINUTES).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SERVER_PATH)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService uploadImage = retrofit.create(ApiService.class);
/* Log.d("hanish12345", fileToUpload1 + " " + filename1);
Log.d("hanish12345", fileToUpload2 + " " + filename2);*/
Call<ProfileResponse> fileUpload1 = uploadImage.uploadFile(fileToUpload1, filename1);
fileUpload1.enqueue(new Callback<ProfileResponse>() {
@Override
public void onResponse(Call<ProfileResponse> call, Response<ProfileResponse> response) {
if (response.isSuccessful()) {
Toast.makeText(AddVehicleFromNavBar.this, "Bill Uploaded " + response.raw().message(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(AddVehicleFromNavBar.this, response.raw().message(), Toast.LENGTH_LONG).show();
}
// Toast.makeText(MainActivity.this, "Success " + response.body().getSuccess(), Toast.LENGTH_LONG).show();
/* Log.d("hanish12345", "No Error ");*/
}
@Override
public void onFailure(Call<ProfileResponse> call, Throwable t) {
if (t instanceof SocketTimeoutException) {
/* Log.d("hanish12345", "Error hai " + t.getMessage());*/
Toast.makeText(getApplicationContext(), "Internet connection not available", Toast.LENGTH_SHORT).show();
}
}
});
Call<ProfileResponse> fileUpload2 = uploadImage.uploadFile(fileToUpload2, filename2);
fileUpload2.enqueue(new Callback<ProfileResponse>() {
@Override
public void onResponse(Call<ProfileResponse> call, Response<ProfileResponse> response) {
if (response.isSuccessful()) {
Toast.makeText(AddVehicleFromNavBar.this, "Vehicle Image Uploaded ! " + response.raw().message(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(AddVehicleFromNavBar.this, response.raw().message(), Toast.LENGTH_LONG).show();
}
// Toast.makeText(MainActivity.this, "Success " + response.body().getSuccess(), Toast.LENGTH_LONG).show();
/*Log.d("hanish12345", "No Error ");*/
}
@Override
public void onFailure(Call<ProfileResponse> call, Throwable t) {
if (t instanceof SocketTimeoutException) {
Toast.makeText(getApplicationContext(), "Internet connection not available", Toast.LENGTH_SHORT).show();
/*Log.d("hanish12345", "Error hai " + t.getMessage());*/
}
}
});
}
TA贡献1831条经验 获得超10个赞
对于 picasso,您必须导入 lib,如果没有 picasso,您可以直接使用 url 填充 imageview
URL url = new URL("give_url_here");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
希望它会有所帮助
添加回答
举报