猿问

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

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


沧海一幻觉
浏览 1373回答 3
3回答

MMMHUHU

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

POPMUISE

如果您使用Picasso或Glide或Universal-Image-Loader从网址加载图像。您可以简单地获取加载的位图毕加索 (当前版本2.71828)Java代码Picasso.get().load(imageUrl).into(new Target() {    @Override    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {        // loaded bitmap is here (bitmap)    }    @Override    public void onBitmapFailed(Drawable errorDrawable) { }    @Override    public void onPrepareLoad(Drawable placeHolderDrawable) {}});Kotlin代码Picasso.get().load(url).into(object : com.squareup.picasso.Target {     override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {         // loaded bitmap is here (bitmap)    }    override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}    override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {}})对于GlideCheck 如何使用滑动将图像下载到位图中?对于Universal-Image-LoaderJava代码imageLoader.loadImage(imageUrl, new SimpleImageLoadingListener() {    @Override    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)     {         // loaded bitmap is here (loadedImage)    }});

喵喔喔

我喜欢这些:从InputStream创建Bitmap并返回它:&nbsp; &nbsp; public static&nbsp; Bitmap downloadImage(String url) {&nbsp; &nbsp; &nbsp; &nbsp; Bitmap bitmap = null;&nbsp; &nbsp; &nbsp; &nbsp; InputStream stream = null;&nbsp; &nbsp; &nbsp; &nbsp; BitmapFactory.Options bmOptions = new BitmapFactory.Options();&nbsp; &nbsp; &nbsp; &nbsp; bmOptions.inSampleSize = 1;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream = getHttpConnection(url);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (IOException e1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e1.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("downloadImage"+ e1.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return bitmap;&nbsp; &nbsp; }&nbsp; // Makes HttpURLConnection and returns InputStream&nbsp;public static&nbsp; InputStream getHttpConnection(String urlString)&nbsp; throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; InputStream stream = null;&nbsp; &nbsp; &nbsp; &nbsp; URL url = new URL(urlString);&nbsp; &nbsp; &nbsp; &nbsp; URLConnection connection = url.openConnection();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpURLConnection httpConnection = (HttpURLConnection) connection;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpConnection.setRequestMethod("GET");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; httpConnection.connect();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream = httpConnection.getInputStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("downloadImage" + ex.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return stream;&nbsp; &nbsp; }请记住:Android包括两个HTTP客户端:HttpURLConnection和Apache HTTP Client。 对于姜饼和后来,HttpURLConnection是最好的选择。从Android 3.x Honeycomb或更高版本,您无法在UI线程上执行网络IO并执行此操作会抛出android.os.NetworkOnMainThreadException。您必须使用Asynctask,如下所示/**&nbsp; &nbsp; &nbsp;AsyncTAsk for Image Bitmap&nbsp; */&nbsp; &nbsp; private class AsyncGettingBitmapFromUrl extends AsyncTask<String, Void, Bitmap> {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected Bitmap doInBackground(String... params) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("doInBackground");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bitmap bitmap = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bitmap = AppMethods.downloadImage(params[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return bitmap;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onPostExecute(Bitmap bitmap) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("bitmap" + bitmap);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Android
Java
我要回答