gpt4 book ai didi

android - 无法使用 Picasso 和 scrollGalleryView 从内存中删除位图

转载 作者:太空狗 更新时间:2023-10-29 14:49:39 24 4
gpt4 key购买 nike

我有一个 Activity ,我在其中加载位图 ScrollGalleryView使用毕加索。当我退出该 Activity 并再次进入时,内存未清空并抛出 OutOfMemory 错误。
我尝试在 Picasso 中使用 .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) 但图像仍然加载并保存在内存中。我也尝试在按下后退按钮时回收位图,但我仍然没有运气。
这是我正在使用的代码:

//Method called in the onCreate that loads the photo in the scrollGalleryView:
private void loadPhotos() {
savedImages = new ArrayList<Uri>();

File file = new File(getPhotoDirectory());
File[] files = file.listFiles();
if (files != null) {
for (File f : files) { // loop and print all file
savedImages.add(Uri.fromFile(f));
}
}

if (!savedImages.isEmpty()) {
for (final Uri savedImage : savedImages) {
if (savedImage.getLastPathSegment().contains(radiatorId)) {
scrollGalleryView.setVisibility(View.VISIBLE);
RadiatorSettingsMediaLoader mMediaLoader = new RadiatorSettingsMediaLoader(savedImage);
scrollGalleryView.addMedia(MediaInfo.mediaLoader(mMediaLoader));
}
}
}
}





class RadiatorSettingsMediaLoader implements MediaLoader {
Uri savedImage;

public RadiatorSettingsMediaLoader(Uri savedImage) {
this.savedImage = savedImage;
}

@Override
public boolean isImage() {
return true;
}

@Override
public void loadMedia(final Context context, final ImageView imageView,
final MediaLoader.SuccessCallback callback) {
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public ViewTreeObserver.OnGlobalLayoutListener getLayoutListener() {
return this;
}

@Override
public void onGlobalLayout() {
Picasso.with(getApplicationContext()).load(savedImage)
.resize(imageView.getWidth(), (imageView.getHeight()) - 175)
.centerInside()
.placeholder(imageView.getDrawable())
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(imageView, new Callback() {
@Override
public void onSuccess() {
callback.onSuccess();
imageView.setPadding(0, 0, 0, 175);
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(getLayoutListener());
Picasso.with(getApplicationContext()).invalidate(new File(savedImage.getPath()));
}

@Override
public void onError() {
Toast.makeText(context, "non sono riuscito a caricare l'immagine", Toast.LENGTH_SHORT).show();
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(getLayoutListener());
loadMedia(context, imageView, callback);

}
});
imageViewsToClear.add(imageView);

}
});
}

@Override
public void loadThumbnail(final Context context, final ImageView thumbnailView,
final MediaLoader.SuccessCallback callback) {


thumbnailView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public ViewTreeObserver.OnGlobalLayoutListener getLayoutListener() {
return this;
}

@Override
public void onGlobalLayout() {
Picasso.with(context)
.load(savedImage)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(thumbnailView, new Callback() {
@Override
public void onSuccess() {

callback.onSuccess();
thumbnailView.setLongClickable(true);
thumbnailView.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(RadiatorSettingsActivity.this);

builder.setTitle(R.string.safe_delete_photo_title).setMessage(R.string.safe_delete_photo_text);
builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File fdelete = new File(savedImage.getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
deleteFileFromMediaStore(getContentResolver(), fdelete);
System.out.println("file Deleted ");
finish();
startActivity(getIntent().putExtra("PhotoRemoved", true));
} else {
System.out.println("file not Deleted :");
}
}


}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
return true;
}
});
thumbnailView.getViewTreeObserver()
.removeOnGlobalLayoutListener(getLayoutListener());
}


@Override
public void onError() {
Toast.makeText(context, "errore a caricare thumbnail", Toast.LENGTH_SHORT).show();
loadThumbnail(context, thumbnailView, callback);
thumbnailView.getViewTreeObserver()
.removeOnGlobalLayoutListener(getLayoutListener());
}
});

imageViewsToClear.add(thumbnailView);
}
});


}

}

最佳答案

在你的程序中调用它

public void clearAllResources() {

// Set related variables null

System.gc();
Runtime.getRuntime().gc();
}

即 clearAllResources(); Activity 开始时

关于android - 无法使用 Picasso 和 scrollGalleryView 从内存中删除位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36398518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com