1.首先确认 URL 是有效的,可以使用浏览器或其他工具来测试。 2. 检查将 URL 加载到 ImageView 的代码是否正确。以下是一个示例:
//在 MainActivity.java 中
public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);
        //创建 Retrofit 实例
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://example.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //创建 API 接口
        APIInterface api = retrofit.create(APIInterface.class);
        //使用 Retrofit 加载图像
        Call call = api.getImage(url);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if(response.isSuccessful()) {
                    //将响应体转换为图像位图
                    Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
                    //将位图设置为 ImageView 的图像
                    imageView.setImageBitmap(bitmap);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                //处理错误情况
            }
        });
    }
}
     3.确保在 AndroidManifest.xml 中添加了 Internet 权限:
使用以上步骤可以成功将 Retrofit Gson 从 URL 中加载图像并在 ImageView 中显示。
                    上一篇:AndroidStudio中使用权限(appcompanist)时,无法进行Compose预览。
                
下一篇:AndroidStudio中使用setOnItemClickListener时出现“(NoClassDefFoundError:Failedresolutionof)”的错误。