为了账号安全,请及时绑定邮箱和手机立即绑定

Android - Picasso 未将 URL 图像加载到

Android - Picasso 未将 URL 图像加载到

收到一只叮咚 2021-10-27 09:41:45
我的毕加索版本是: implementation 'com.squareup.picasso:picasso:2.71828'我正在尝试将以下图像加载到我的 ImageView天气图标中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 等。
查看完整描述

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

                        }

                    }

                });


        }


查看完整回答
反对 回复 2021-10-27
?
慕哥6287543

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);

希望它会有所帮助


查看完整回答
反对 回复 2021-10-27
  • 3 回答
  • 0 关注
  • 140 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信