当我第一次运行我的应用程序时,没有创建文件,但是当我再次调用该方法时,它可以正常工作。
这是我打电话刷新的时候:
List<Product> prod = new Select().from(Product.class).queryList();
for (Product products:prod){
ImageCache imageCache = new ImageCache();
Log.i("product", products.getProductDescr());
imageCache.setProductName(products.getProductName());
Picasso.with(this)
.load(products.getProductImage())
.into(imageCache.getTarget());
}
这是我用方法调用的类:
public class ImageCache {
public String productName;
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductName() {
return productName;
}
public Target getTarget() {
return target;
}
public void setTarget(Target target) {
this.target = target;
}
public Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory().getPath() +"/Thesis/" +productName+ ".jpg");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
if (placeHolderDrawable != null) {
}
}
};}
解: 创建目标时必须保留强引用以避免垃圾回收。这将通过此示例
成功完成public class MyClass {
private Target mTarget = new Target() {...};
public void getPointMarkerFromUrl(final String url, final OnBitmapDescriptorRetrievedListener listener) {
Picasso.with(context)
.load(url)
.resize(maxSize, maxSize)
.into(mTarget);
}
}