Android的负荷网址为位图位图、负荷、网址、Android

由网友(屋顶上的拾荒人)分享简介:我有一个关于从网站加载图像的问题。在code我用的是:显示显示= getWindowManager()getDefaultDisplay()。INT宽度= display.getWidth();INT高= display.getHeight();位图位= NULL;尝试 {位= BitmapFactory.de...

我有一个关于从网站加载图像的问题。在code我用的是:

 显示显示= getWindowManager()getDefaultDisplay()。
INT宽度= display.getWidth();
INT高= display.getHeight();
位图位= NULL;
尝试 {
    位= BitmapFactory.de codeStream((InputStream的)新URL("http://www.mac-wallpapers.com/bulkupload/wallpapers/Apple%20Wallpapers/apple-black-logo-wallpaper.jpg").getContent());
}赶上(例外五){}
位图SC = Bitmap.createScaledBitmap(位,宽度,高度,真正的);
canvas.drawBitmap(SC,0,0,NULL);
 

但它总是返回一个空指针异常,程序崩溃了。 URL是有效的,而且它似乎工作的其他人。 我使用的是2.3.1。

解决方案

 公共静态位图getBitmapFromURL(字符串SRC){
    尝试 {
        网址URL =新的URL(SRC);
        HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
        connection.setDoInput(真正的);
        connection.connect();
        输入的InputStream = connection.getInputStream();
        位图MYBITMAP = BitmapFactory.de codeStream(输入);
        返回MYBITMAP;
    }赶上(IOException异常E){
        //记录异常
        返回null;
    }
}
 

使用Hbuilder打包前端网站为WebApp Android,ios应用

I have a question about loading an image from a website. The code I use is:

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

But it always returns a null pointer exception and the program crashes out. The URL is valid, and it seems to work for everyone else. I'm using 2.3.1.

解决方案

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        // Log exception
        return null;
    }
}

阅读全文

相关推荐

最新文章