Java Android-毕加索没有将URL图像加载到Fragment ImageView中
我的毕加索版本是:实现'com.squareup.Picasso:Picasso:2.71828' 我正在尝试将以下图像加载到我的ImageView weatherIcon中 这是我正在尝试加载的图像 上面图像的URL 我的Tab1Fragment代码Java Android-毕加索没有将URL图像加载到Fragment ImageView中,java,android,picasso,Java,Android,Picasso,我的毕加索版本是:实现'com.squareup.Picasso:Picasso:2.71828' 我正在尝试将以下图像加载到我的ImageView weatherIcon中 这是我正在尝试加载的图像 上面图像的URL 我的Tab1Fragment代码 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceSta
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1_fragment, container, false);
weatherIcon = (ImageView) rootView.findViewById(R.id.imageView);
return rootView;
}
但是,没有运气。图像没有加载,有趣的是,毕加索语句停止了以下所有语句,例如文本视图中的SetText等。使用
iconUrl ="http://openweathermap.org/img/w/04n.png"
Picasso.with(context).load(iconUrl).placeholder(R.drawable.user_image)// Place
holder image from drawable folder
.error(R.drawable.user_image).resize(110, 110).centerCrop()
.into(weatherIcon);
对于毕加索,您必须导入库,没有毕加索,您可以直接使用url填充imageview
URL url = new URL("give_url_here");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
希望有助于在使用Picasso的地方发布完整代码。是否从“活动”加载此图像?My ImageView变量在单独的Java文件中声明,并链接到布局。我正在通过另一个文件访问这些文件。这已经用于设置文本视图的文本,而不是毕加索图像视图。请提供Tab1Fragment的完整代码以及使用此Picasso.get.loadiconUrl.intoTab1Fragment.weatherIcon的类;代码。请参阅我的代码以使用毕加索。您必须在gradle中添加库,正如我添加的:实现'com.squareup.picasso:picasso:2.5.0'
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());*/
}
}
});
}
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());*/
}
}
});
}