首页 > 解决方案 > Android - Picasso 未将 URL 图像加载到 Fragment ImageView

问题描述

我的毕加索版本是:implementation 'com.squareup.picasso:picasso:2.71828'

我正在尝试将以下图像加载到我的 ImageView weatherIcon

iconUrl = "http://openweathermap.org/img/w/"+icon+".png";
Picasso.get().load(iconUrl).into(Tab1Fragment.weatherIcon);

这是我要加载的图像

上图的网址

http://openweathermap.org/img/w/04n.png

我的 Tab1Fragment 代码

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;

}

但是,没有运气。图像没有加载,有趣的是,毕加索语句停止了所有后续语句,例如 TextViews 中的 SetText 等。

标签: javaandroidpicasso

解决方案


  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());*/
                        }
                    }
                });

        }

推荐阅读