Android 使用毕加索下载图像

Android 使用毕加索下载图像,android,picasso,Android,Picasso,我有一个图像的URL列表。我想用毕加索把这些图像下载到安卓系统的本地存储中。我使用以下方法(使用自定义目标)。但是我没有看到所有的图片都被下载 public void downloadAllMapImages(List<ProjectMapModel> models) { List<Target> targetList=Lists.newArrayList(); for(ProjectMapModel model:models){ for

我有一个图像的URL列表。我想用毕加索把这些图像下载到安卓系统的本地存储中。我使用以下方法(使用自定义目标)。但是我没有看到所有的图片都被下载

public void downloadAllMapImages(List<ProjectMapModel> models) {
    List<Target> targetList=Lists.newArrayList();
    for(ProjectMapModel model:models){
        for (Map.Entry<Integer, String> entry : model.getMaps().entrySet()) {
            String url=entry.getValue();
            Target target=mapImageTarget(model.getProjectId(),entry.getKey());
            targetList.add(target);
            Picasso.with(this)
                    .load(url)
                    .into(target);
        }
    }
    if(!isImageDownloadSuccessful)
        showDownloadMapImageDataError();

    //whatever images were downloaded, we need to write them to database
    listDetailPresenter.onImageDownloadingSuccessful(projectMapOutputModels);
}

  private Target mapImageTarget(final int projectId, final int mapType) {
    //create the file name
    final File file = new File(this.getExternalFilesDir(null) + File.separator + projectId+"_"+ mapType + ".jpg");
    final String filePath=file.getAbsolutePath();
    return new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            // loading of the bitmap was a success
            // write the image to file
            new Thread(new Runnable() {
                @Override
                public void run() {
                    OutputStream outputStream = null;
                    try {
                        outputStream = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                    } catch (IOException e) {
                        Log.e(DashboardActivity.class.getName(), e.toString());
                        isImageDownloadSuccessful=false;
                    } finally {
                        try {
                            outputStream.close();
                            Log.i(DashboardActivity.class.getName(), "image saved");
                        } catch (IOException e) {
                            Log.e(DashboardActivity.class.getName(), e.toString());
                            isImageDownloadSuccessful=false;
                        }
                    }
                    projectMapOutputModels.add(new ProjectMapOutputData(projectId,filePath,mapType));
                    Log.i("image", "image saved to >>>" + filePath);

                }
            }).start();

            //notify presenter that this image has been download and the path can be sent to presenter
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            // loading of the bitmap failed
            // TODO show error message toast. dont write file path to database. continue with next image
            isImageDownloadSuccessful=false;
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };

}
public void下载所有地图图像(列表模型){
List targetList=Lists.newArrayList();
用于(ProjectMapModel模型:模型){
对于(Map.Entry:model.getMaps().entrySet()){
字符串url=entry.getValue();
Target Target=mapImageTarget(model.getProjectId(),entry.getKey());
targetList.add(目标);
毕加索。用(这个)
.load(url)
.进入(目标);
}
}
如果(!isImageDownloadSuccessful)
showDownloadMapImageDataError();
//无论下载了什么图像,我们都需要将它们写入数据库
onImageDownloadingSuccessful(projectMapOutputModels);
}
专用目标mapImageTarget(最终int projectId,最终int mapType){
//创建文件名
final File File=新文件(this.getExternalFilesDir(null)+File.separator+projectId+“”+mapType+”.jpg”);
最终字符串filePath=file.getAbsolutePath();
返回新目标(){
@凌驾
公共无效onBitmapLoaded(最终位图,Picasso.LoadedFrom){
//位图加载成功
//将图像写入文件
新线程(newrunnable()){
@凌驾
公开募捐{
OutputStream OutputStream=null;
试一试{
outputStream=新文件outputStream(文件);
compress(bitmap.CompressFormat.JPEG,100,outputStream);
}捕获(IOE异常){
Log.e(DashboardActivity.class.getName(),e.toString());
isImageDownloadSuccessful=false;
}最后{
试一试{
outputStream.close();
Log.i(DashboardActivity.class.getName(),“图像已保存”);
}捕获(IOE异常){
Log.e(DashboardActivity.class.getName(),e.toString());
isImageDownloadSuccessful=false;
}
}
添加(新的ProjectMapOutputData(projectId、filePath、mapType));
Log.i(“图像”,“图像保存到>>>”+文件路径);
}
}).start();
//通知演示者此图像已下载,并且可以将路径发送给演示者
}
@凌驾
公共无效onBitmapFailed(Drawable errorDrawable){
//加载位图失败
//TODO显示错误消息toast。不要将文件路径写入数据库。继续下一个映像
isImageDownloadSuccessful=false;
}
@凌驾
准备加载时的公共无效(可提取占位符可提取){
}
};
}

我遗漏了什么吗?非常感谢您的帮助

你能详细解释一下你对
的意思吗?我看不到所有的图片都被下载了。
只有一些图片成功了吗?好的,我成功了。我必须删除新线程部分,并将magic inside Run方法移到新线程之外。但我想在下载完所有图像后采取一些行动或通知演示者。我怎么知道所有的图片什么时候都被下载了。终于让它工作了!你能详细解释一下你对
的意思吗?我看不到所有的图片都被下载了。
只有一些图片成功了吗?好的,我成功了。我必须删除新线程部分,并将magic inside Run方法移到新线程之外。但我想在下载完所有图像后采取一些行动或通知演示者。我怎么知道所有的图片什么时候都被下载了。终于让它工作了!