2016-09-08 25 views
0

我从我的服务器获取图像网址列表,并使用适配器将它们设置在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); 

     } 
    }); 

} 

回答

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) { 
     } 
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) 
0

例与滑翔库,在这里你有一个例子:

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)比当图像被成功加载库回调将返回你位图图象OR 2 )您只能同步获取位图。

实施例与通用图像装载机文库(来自库的文档):

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

当我在我的onBindviewholder方法中使用相同的方法时,它将错误显示为java.lang.IllegalArgumentException:YOu必须在后台线程上调用此方法 – user2645941

+0

我刚刚编辑了我的答案,请检查Glide第二个示例。 –