【发布时间】:2012-04-04 21:51:02
【问题描述】:
目前我正在制作一个应用来显示 SD 卡中的图像。基本上你制作一个相册并向其中添加图片,无论是来自 MediaStore 选择器的相机。
我尝试了两种方法:
- 带有自定义 BaseAdapter 以返回视图的标准图库应用
- 带有自定义 PagerAdapter 的 viewpager
我不想显示网格视图,所以它应该立即全屏显示。之后我想禁用左右滑动,只听点击。
Atm 两种方法都在纵向模式下工作。当我切换到横向时,一些图像就会掉下来
03-20 12:20:56.515: W/OpenGLRenderer(17398): Bitmap too large to be uploaded into a texture
接着是内存不足错误。如果 OOM 和图库出现问题,堆栈溢出,您应该回收视图以使其正常工作,因为 baseAdapter 的 getView 中的 convertView 始终为空。
所以我为视图使用了回收器,我将其限制为 2 个视图,并且纵向模式适用于方法 1(使用图库)。景观仍然给我同样的问题。
对于方法 2 (viewflipper),它通过
处理视图@Override
public void destroyItem(ViewGroup container, int position, Object object) {
顺便说一句,Wich 从来没有被称为...但是纵向模式在这里有效。景观仍然崩溃。
我获取位图的方法:
public static Bitmap getBitmap(Context ctx, int imageId, ImageView target) {
String file = getPath(ctx, imageId);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bmOptions);
WindowManager mgr = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
int scaleFactor = 1;
if (mgr != null) {
// Get the dimensions of the View
int targetW = mgr.getDefaultDisplay().getWidth();
int targetH = mgr.getDefaultDisplay().getHeight();
Log.d(TAG, "Image width + height=" + targetW + "," + targetH);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
scaleFactor = Math.min(photoW / targetW, photoH / targetH);
} else {
Log.d(TAG, "Target is null");
}
// Get the dimensions of the bitmap
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
logHeap(ImageHelper.class);
Bitmap bm = BitmapFactory.decodeFile(file, bmOptions);
if (target != null) {
target.setImageBitmap(bm);
}
return bm;
}
工作正常,我知道我使用窗口管理器来获取屏幕尺寸,但那是因为我的 ImageView 在我膨胀时仍然是 size(0,0)。然后我打电话给
imgView.setLayoutParams(new Gallery.LayoutParams(
Gallery.LayoutParams.FILL_PARENT,
Gallery.LayoutParams.FILL_PARENT));
imgView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
似乎没有任何效果...
请注意,我不使用可绘制对象或 APK 中包含的任何其他资源。 这个画廊应该能够以任何质量从 SD 卡或相机加载图片。显然,画廊应该能够处理与目录中一样多的图像。
有人可以帮我解决以下问题吗?
- 有什么方法可以让默认图库立即全屏显示并屏蔽网格视图?这样我只需要一个适配器来提供图像而不是制作我自己的视图。 (也许这可以解决 OOM 崩溃问题)
- 我的位图解码功能正常吗?我需要内置一些技巧来捕捉景观变化吗?
- 使用浏览器或画廊制作我需要的画廊的最佳方式是什么?
- 有没有人有一些不崩溃的全屏画廊的示例代码?
【问题讨论】:
-
您是在尝试创建画廊还是尝试访问画廊?解码逻辑看起来也不错。
标签: android android-viewpager out-of-memory gallery android-gallery