Picasso不会在我公司的服务器上下载Image for First Time吗?

问题描述 投票:0回答:1

当ImagePager第一次加载时,Picasso会调用onError,显示.error drawable。如果我按下后退按钮并返回到具有ImagePager的Activity,Picasso会正确加载图片。如果ImagePager有两张或更多图片,我在图片之间滑动,这些图片会正确加载一段时间而不退出并重新进入ImagePager。

它正确地从网上下载其他图像。当我尝试从托管公司服务器下载时出现问题。

我正在使用Picasso'com.squareup.picasso:picasso:2.5.0'。

我也提到了下面的问题,但它没有帮助。

First time error loading picture with Picasso

下面是我的MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        configureToolbar(R.string.select_task);
        init();
    }

@Override
    protected void init() {
        //TODO: For QA Testing Purpose, Remove after Testing
        mWidthField =findViewById(R.id.edt_txt_1);
        mHeightField =findViewById(R.id.edt_txt_2);
        mImage=findViewById(R.id.image_view_2);
        mImageLoadButton=findViewById(R.id.image_load_button);
        item=new Item();
        item.setPrimaryImageURL("https://cdn.cnn.com/cnnnext/dam/assets/190119161516-01-trump-government-shutdown-0119-exlarge-169.jpg");
        item.setUpc("0001111086751");
        Log.d("ImageManager","Main Activity");
        new ImageManager(getApplicationContext()).downloadImage(item.getPrimaryImageURL(),item.getUpc()+".jpeg",imageDownloadedCallBack);
        mImageLoadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadResizedImage();
            }
        });

    }


//TODO: For QA Testing Purpose, Remove after Testing
    ImageManager.ImageDownloadedCallBack imageDownloadedCallBack=new ImageManager.ImageDownloadedCallBack() {
        @Override
        public void imageDownloadComplete(final Bitmap bitmap, boolean status) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.d("ImageManager","Image Download CallBack in Main Activity");
                    mImage.setImageBitmap(bitmap);

                }
            });
        }
    };

下面是我的ImageManager.java

public class ImageManager {
    private final Context mContext;
    private int mWidth;
    private int mHeight;
    public ImageManager(Context mContext){
        this.mContext=mContext;
    }
    public interface ImageDownloadedCallBack {
        void imageDownloadComplete(Bitmap bitmap,boolean status);
    }

    private Target picassoImageTarget(Context context, final String imageDir, final String imageName,final ImageDownloadedCallBack imageDownloadedCallBack) {
        ContextWrapper cw = new ContextWrapper(context);
        final File directory = cw.getDir(imageDir, Context.MODE_PRIVATE); // path to /data/data/yourapp/app_imageDir
        return new Target() {
            @Override
            public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
                Thread thread=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        final File myImageFile = new File(directory, imageName);
                        FileOutputStream fos = null;
                        try {
                            fos = new FileOutputStream(myImageFile);
                            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                            Log.d("ImageManager","Image DownLoad CallBack");
                            imageDownloadedCallBack.imageDownloadComplete(bitmap,true);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                fos.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                thread.start();
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
                Log.d("ImageManager","Bitmap Failure");
            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
                if (placeHolderDrawable != null) {}
            }
        };
    }

public void downloadImage(String url, String id,ImageDownloadedCallBack imageDownloadedCallBack){
//        this.imageDownloadedCallBack=imageDownloadedCallBack;
        Log.d("ImageManager","Download Image function");
        Picasso.with(mContext).load(url).into(picassoImageTarget(mContext,"imageDir", id ,imageDownloadedCallBack));

    }
}

任何帮助,将不胜感激。

java android multithreading picasso
1个回答
0
投票

我找到了旧帖子的答案,我在MainActivity中实现了这段代码。现在它运作良好。

final Target target = new Target{...};
imageView.setTag(target);

请参阅下面的wrb-answer以获取上述代码:

wrb-answer

© www.soinside.com 2019 - 2024. All rights reserved.