相关疑难解决方法(0)

Android从URL加载到Bitmap

我有一个关于从网站加载图像的问题.我使用的代码是:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();
Bitmap bit=null;
try {
    bit = BitmapFactory.decodeStream((InputStream)new URL("http://www.mac-wallpapers.com/bulkupload/wallpapers/Apple%20Wallpapers/apple-black-logo-wallpaper.jpg").getContent());
} catch (Exception e) {}
Bitmap sc = Bitmap.createScaledBitmap(bit,width,height,true);
canvas.drawBitmap(sc,0,0,null);
Run Code Online (Sandbox Code Playgroud)

但它总是返回一个空指针异常,程序崩溃了.该URL有效,似乎适用于其他所有人.我正在使用2.3.1.

java android bitmap

68
推荐指数
5
解决办法
14万
查看次数

如何将图像 url 转换为 Drawable Int

大家早上好 !!!!

我正在尝试在 Imageview 中显示图像。但我的要求是,我需要以可绘制整数格式显示图像。我从服务器获取数据并以 url 格式获取图像..

For ex :http://www.mymartmycart.com/images/detailed/3/4G_CONNECT_M1.jpeg
Run Code Online (Sandbox Code Playgroud)

我想将此图像转换为 Drawable 格式。

这是我的代码

这是我的界面

 public interface ECCardData<T> {

    @DrawableRes
    Integer getMainBackgroundResource();

    @DrawableRes
    Integer getHeadBackgroundResource();

    List<T> getListItems();
}
Run Code Online (Sandbox Code Playgroud)

我的吸气剂和吸气剂

     private Integer headBackgroundResource;

public Integer getHeadBackgroundResource() {
        return headBackgroundResource;
    }

    public void setHeadBackgroundResource(Integer headBackgroundResource) {
        this.headBackgroundResource = headBackgroundResource;
    }
Run Code Online (Sandbox Code Playgroud)

我在其中设置图像的代码

Integer drawableRes = dataset.get(position).getHeadBackgroundResource();
    if (drawableRes != null) {
        headView.setHeadImageBitmap(BitmapFactory.decodeResource(pagerContainer.getResources(), drawableRes, new BitmapFactoryOptions()));
    }
Run Code Online (Sandbox Code Playgroud)

这是我的解析

 public static ArrayList<CardData> ParseCraft(String response) throws JSONException {


        ArrayList<CardData> alUser = new ArrayList<>();

        JSONObject jsonRoot = …
Run Code Online (Sandbox Code Playgroud)

android json

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×2

bitmap ×1

java ×1

json ×1