Android从毕加索获取图像到位图阵列

Android从毕加索获取图像到位图阵列,android,bitmap,picasso,Android,Bitmap,Picasso,我想从带有链接的字符串[]中获取位图[]。但这不是我想要的。我有这个方法: private Bitmap[] getBitmaps(String[] images){ ArrayList<Bitmap> temp = new ArrayList<>(); for(int i = 0; i < images.length; i++){ ImageView img = new ImageView(getContext());

我想从带有链接的字符串[]中获取位图[]。但这不是我想要的。我有这个方法:

private Bitmap[] getBitmaps(String[] images){
    ArrayList<Bitmap> temp = new ArrayList<>();
    for(int i = 0; i < images.length; i++){
        ImageView img = new ImageView(getContext());
        FrameLayout.LayoutParams x = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        img.setLayoutParams(x);
        Picasso.with(getContext()).load(MainPostAdapter.USER_URL+images[i]+".png").into(img, new Callback() {
            @Override
            public void onSuccess() {
                temp.add(BitmapRes.drawableToBitmap(img.getDrawable()));
                movableBackgroundContainer.removeView(img);
            }

            @Override
            public void onError() {

            }
        });
        movableBackgroundContainer.addView(img);
    }
    return temp.toArray(new Bitmap[temp.size()]);
}
私有位图[]获取位图(字符串[]图像){
ArrayList temp=新的ArrayList();
对于(int i=0;i

问题是我得到了一个空数组,因为它在onSuccess函数之后将位图添加到列表中。我现在如何等到all onSuccess添加位图后返回?

每次成功后,您都可以增加一个整数,直到整数等于images.lengh()。你可以用一个循环来检查。在循环中,返回中有一个if子句

比如说

int currentSuccess = 0;
在循环中:

     @Override
            public void onSuccess() {
                temp.add(BitmapRes.drawableToBitmap(img.getDrawable()));
                movableBackgroundContainer.removeView(img);
                currentSuccess++;
            }
至于报税表:

 while(true){
     if(currentSuccess == images.length){
        return temp.toArray(new Bitmap[temp.size()]);
     }
}
希望能有所帮助。

毕加索的
get()
功能满足您的需求。它下载位图,而不是将图像加载到ImageView中。请注意,不能在主线程上调用毕加索的
get()
方法。我的示例使用AsyncTask在单独的线程上下载图像

    String[] images = new String[] {"http://path.to.image1.jpg", "http://path.to.image2.jpg"};
    new AsyncTask<String[], Void, List<Bitmap>>() {
        @Override
        protected List<Bitmap> doInBackground(String[]... params) {
            try {
                List<Bitmap> bitmaps = new ArrayList<Bitmap>();
                for (int i = 0; i < params[0].length; ++i) {
                    bitmaps.add(Picasso.with(getActivity()).load(params[0][i]).get());
                }
                return bitmaps;
            } catch (IOException e) {
                return null;
            }
        }

        @Override
        public void onPostExecute(List<Bitmap> bitmaps) {
            if (bitmaps != null) {
                // Do stuff with your loaded bitmaps
            }
        }
    }.execute(images);
String[]images=新字符串[]{”http://path.to.image1.jpg", "http://path.to.image2.jpg"};
新建异步任务(){
@凌驾
受保护列表doInBackground(字符串[]…参数){
试一试{
列表位图=新的ArrayList();
对于(int i=0;i