无法将图片网址转换为位图

时间:2016-09-08 08:48:37

标签: android

我从我的服务器获取图像的URL列表并使用适配器在recyclerview中设置它们。我的要求是从图像中获取颜色并将该颜色设置为textview的背景,因为我使用的是调色板库如下所示。但是我收到错误的InputStream行无法排序问题,任何帮助将不胜感激。

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {

    Album albm=malbumlist.get(position);
    holder.title.setText(albm.getProductName());
    System.out.println("Displaying the image on Recycleview");
    Glide.with(mcontext).load(albm.getImageUrl()).into(holder.thumbnail);

try {
        URL url=new URL(albm.getImageUrl());
        HttpURLConnection connection= (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream is=connection.getInputStream();    /*  Getting error here */
        bmp=BitmapFactory.decodeStream(is);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Palette.generateAsync(bmp, new Palette.PaletteAsyncListener() {
        @Override
        public void onGenerated(Palette palette) {

            int bgcolor=palette.getVibrantColor(mcontext.getResources().getColor(android.R.color.black));
            holder.linearLayout.setBackgroundColor(bgcolor);

        }
    });

}

3 个答案:

答案 0 :(得分:0)

检查一下......我正在使用这个..如果它有用,请将此答案正确。

String path = imageToUploadUri.getPath();
        File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.RGB_565;

        Bitmap b = BitmapFactory.decodeFile(path, options);
        int bmpHeight = b.getHeight();
        int bmpWidth = b.getWidth();
        if (bmpHeight > 1500) {
            bmpHeight = bmpHeight / 4;
            bmpWidth = bmpWidth / 4;
        }
        Bitmap out = Bitmap.createScaledBitmap(b, bmpWidth, bmpHeight, false);

        File file = new File(dir, "resize.png");
        FileOutputStream fOut;
        try {
            fOut = new FileOutputStream(file);
            out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
            b.recycle();
            out.recycle();
        } catch (Exception e) {
        }

答案 1 :(得分:0)

试试这段代码

 /**
     * load image from url and fill it in image view
     *
     * @param context
     * @param imageView   the view which will be filled by the bitmap result
     * @param imageUrl    the image link
     * @param imageHolder this drawable for place holder image
     */
    public Request loadImage(Context context, ImageView imageView, String imageUrl, @DrawableRes int imageHolder) {
        GlideUrl glideUrl = new GlideUrl(imageUrl, new LazyHeaders.Builder()
                .build());
         (imageHolder == 0)
            imageHolder = R.drawable.error_image;

        return Glide.with(context)
                .load(glideUrl)
                .error(imageHolder)
                .into(imageView).getRequest();
    }

这是我对方法的调用

loadImage(context, imageView,imageUrl, 0)

答案 2 :(得分:0)

Glide 库的示例,这里有一个示例:

Bitmap bitmap = Glide.
        with(MainActivity.this).
        load("https://YourLink").
        asBitmap().
        into(50, 50).
        get();

滑翔第二个例子:

Glide.with(getApplicationContext())
     .load("https://YourLink")
     .asBitmap()
     .into(new BitmapImageViewTarget(imgView) {
      @Override
      protected void setResource(Bitmap resource) {
       //Your bitmap here!
        super.setResource(resource);
      }
    });

此处的另一个解决方案是使用Android-Universal-Image-Loader库来加载来自URL的图像 1),而不是成功加载图像时,库回调将为您返回该图像的位图或 2 )你只能同步获得Bitmap。

通用图像加载器库示例(来自库文档):

ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance

案例1:

// Load image, decode it to Bitmap and return Bitmap to callback
imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() {
    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        // Do whatever you want with Bitmap
    }
});

<强>情况2:

// Load image, decode it to Bitmap and return Bitmap synchronously
Bitmap bmp = imageLoader.loadImageSync(imageUri);