Android-Picasso无法将URL图像加载到片段ImageView中

时间:2018-10-03 11:00:30

标签: java android picasso

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

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

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

这是我要加载的图像

上面图片的URL

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 个答案:

答案 0 :(得分:0)

使用

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

答案 1 :(得分:0)

对于毕加索,您必须导入lib,而无需毕加索,您可以直接使用url填充imageview

URL url = new URL("give_url_here");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);

希望这会有所帮助

答案 2 :(得分: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());*/
                        }
                    }
                });

        }