我有一个应用程序从服务器下载图像,将它们存储在SD卡上,然后以幻灯片形式显示给用户.该应用程序有许多不同的幻灯片,用户可以查看.我的问题是,在我查看几个画廊后,我发现内存不足错误.我做了很多谷歌搜索,并阅读了Romain Guy的Avoiding Memory Leaks文章大约20次.我试图复制他的unbindDrawables()方法,但是无法在Gallery对象上调用removeAllViews().我也尝试在所有位图上调用recycle()但是当我在适配器中的位图上执行时,应用程序会在打开图库时立即抛出错误.我也尝试重新编码幻灯片,以便在适配器外部创建所有位图,然后将它们作为数组传递 - 这允许我遍历我的位图数组并在幻灯片中的onDestroy方法中对每个位图调用recycle()活动 - 但这实际上似乎使泄漏变得更糟.
以下是我的幻灯片活动的代码:
public class Slideshow extends Activity {
static String galleryId;
public static final int MSG_DOWNLOADED = 0;
static Handler handler;
static Gallery g;
static ArrayList<String> filePaths;
static String subFolder;
static ArrayList<String> imageToGet;
static LinearLayout pb;
static boolean firstTime = true;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slideshow);
Context context = getApplicationContext();
Bundle extras = getIntent().getExtras();
if(extras != null){
galleryId = extras.getString("galleryId");
}
pb = (LinearLayout) findViewById(R.id.progress);
handler = new Handler(){
@Override
public …Run Code Online (Sandbox Code Playgroud) 我想知道我是否可以在内存中保存当前视口的位图,然后在下一个绘制周期中简单地将该内存绘制到视口中?
我在屏幕的256x256区域绘制了大量数据点作为2D散点图,理论上我可以在每帧中渲染整个图,但在我的情况下,它需要我存储大量数据点(50K) -100K)其中大部分都是冗余的,因为256x256盒子只有~65K像素.
因此,不是重绘和渲染整个场景,t而是想拍摄场景的快照t-1并首先绘制,然后我可以在其上绘制更新.
这可能吗?如果是这样我怎么能这样做,我已经看了很多关于如何做到这一点的线索,但我找不到任何有意义的东西.
我得到"位图大小超过VM预算",最终我的应用程序.所以我添加了所有这些东西来帮助缓解不断增加的内存占用
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[32*1024];
options.inDither=false; //Disable Dithering mode
options.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
options.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
options.inPreferredConfig = Bitmap.Config.RGB_565;
Drawable pulled = BitmapDrawable.createFromResourceStream(null, null, conn.getInputStream(), "galleryImage", options);
Run Code Online (Sandbox Code Playgroud)
我也在使用weakhashmaps,回收,System.gc()
而这一切都成功地延长了坠机事故.最初具有32M堆的设备在崩溃之前只能处理一些图像,现在它可以处理十几个,但这并不能解决问题.问题是BitMap方法泄漏内存并且根本无法清除.
我该如何解决?图像大小为256x357

如何保存(如图所示)保存的部分bitmap(imageview如图所示).我需要做的就是crop a particular image使用(x,y)坐标.所有我拥有的x,y coordinates,没有别的选择cropped area.之间,用户不选择crop area.所以它足够了static.yellow part is an imageview(not the whole activity)
我最近在Android上关注了Googles Advice on Bitmap Caching,并 从支持库中添加了我自己的LruCache.如果缓存已知图像的大小(以字节为单位),则LruCache的效果最佳.
问题是,位图的getByteCount仅在Android API Level 11之后可用.
您如何猜测内存中位图的大小?
我想知道.Net下,如果32BPP真彩色PNG显示在旧显示卡上怎么办?我相信PNG文件中没有调色板.
谁知道这种情况背后的内部逻辑?
我有一个BMP图像,我希望将其转换为Android中的Bitmap对象.谁能解释我怎么做到这一点?
我更改了Android 2.3.4复选框的位图,并希望设置位图的大小以缩放它们.我改变了这样的复选框:
<CheckBox
android:id="@+id/cb1"
android:button="@drawable/checkbox"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sort: 1" />
Run Code Online (Sandbox Code Playgroud)
与匹配的xml drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true" android:drawable="@drawable/checkbox_on_background_focus_yellow"/>
<item android:state_checked="false" android:state_focused="true" android:drawable="@drawable/checkbox_off_background_focus_yellow"/>
<item android:state_checked="false" android:drawable="@drawable/checkbox_off_background" />
<item android:state_checked="true" android:drawable="@drawable/checkbox_on_background" />
</selector>
Run Code Online (Sandbox Code Playgroud)
如何设置dip/dp值来更改复选框位图的大小?
有人可以给我简短的解释如何使用GDI/GDI +创建位图运行时并用颜色填充它?
提前致谢.
我想要实现的是能够从输入流计算位图的高度和宽度,而无需实际更改BitmapFactory.Options
这就是我做的:
private Boolean testSize(InputStream inputStream){
BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options();
Bitmp_Options.inJustDecodeBounds = true;
BitmapFactory.decodeResourceStream(getResources(), new TypedValue(), inputStream, new Rect(), Bitmp_Options);
int currentImageHeight = Bitmp_Options.outHeight;
int currentImageWidth = Bitmp_Options.outWidth;
if(currentImageHeight > 200 || currentImageWidth > 200){
Object obj = map.remove(pageCounter);
Log.i("Page recycled", obj.toString());
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
现在这里的主要问题是它将BitmapFactory.Options更改为无法正确解码的状态.
我的问题是有另一种重置BitmapFactory.Options的方法吗?或其他可能的解决方案
另一种方法:(注意:当应用top方法时,originalBitmap为null)
这是我原来的代码:
Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream);
Run Code Online (Sandbox Code Playgroud)
应用Deev和Nobu Game的建议:(没有变化)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream,null,options);
Run Code Online (Sandbox Code Playgroud)