Reputation: 14067
android remove or recycle images from gallery? Is it possible?
How Can i do this? (g is my gallery view)
I tried this:
private void exit() {
g.setAdapter(null);
for (int i=0; i< imgadapter.images.size(); i++) {
if (imgadapter.images.get(i)!=null) {
imgadapter.images.get(i).recycle();
imgadapter.images.set(i, null);
}
}
imgadapter.images.clear();
System.gc();
}
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): java.lang.NullPointerException
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.widget.Gallery.makeAndAddView(Gallery.java:748)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.widget.Gallery.layout(Gallery.java:625)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.widget.Gallery.onLayout(Gallery.java:339)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.view.View.layout(View.java:7228)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.view.View.layout(View.java:7228)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.view.View.layout(View.java:7228)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.view.View.layout(View.java:7228)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.view.ViewRoot.performTraversals(ViewRoot.java:1145)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.os.Looper.loop(Looper.java:130)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at java.lang.reflect.Method.invoke(Method.java:507)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-05 09:09:19.671: ERROR/AndroidRuntime(1279): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 1721
Reputation: 77762
It's probably not a good idea to carpet-bomb your adapter and delete everything, regardless of whether or not it's still in use. recycle
is merciless and will delete your bitmap, even if it's still in use.
Since you're using a Gallery, I would make use of Android's built-in recycling mechanism for adapters.
See how Adapter.getView
takes a View
parameter. If that's not null, you are recycling an old view. That's the perfect place for you to grab the bitmap from that old view (which you know will not be used anymore!) and recycle it. You can then use that view and re-populate it (rather than creating a new one).
Upvotes: 2