我正在开发一个演示应用程序,其中我正在播放如此多的图像(位图).我使用Global Bitmap对象将同一图像显示到多个Activity中.但是当我尝试使用Bitmap.createBitmap(...)创建Bitmap时,我正在使用OutOfMemoryError.我尝试使用此代码但仍然遇到相同的错误,它正在崩溃我的应用程序并通过OutOfMemoryError.
我坚持这个.谁能有任何解决方案来避免这种情况.
提前致谢.
裁剪图像后我得到位图,以便我使用下面的代码来解码位图大小.
public static Bitmap loadFromBitmap( Context context, Bitmap b, int maxW, int maxH ) throws IOException {
final BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = null;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inJustDecodeBounds = true;
BufferedInputStream stream = null;
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
b.compress(CompressFormat.JPEG, 100, outstream);
InputStream is = new java.io.ByteArrayInputStream(outstream.toByteArray());
stream = new BufferedInputStream(is, 16384 );
if ( stream != null ) {
options.inSampleSize = calculateInSampleSize( options, maxW, maxH );
stream = null;
stream = new BufferedInputStream(is, 16384 );
} else {
return null;
}
options.inDither = false;
options.inJustDecodeBounds = false;
options.inPurgeable = true;
bitmap = BitmapFactory.decodeStream( stream, null, options );
if ( bitmap != null ) bitmap = BitmapUtils.resizeBitmap( bitmap, maxW, maxH );
return bitmap;
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
Run Code Online (Sandbox Code Playgroud)
错误:
02-21 15:32:31.160: E/AndroidRuntime(2951): FATAL EXCEPTION: main
02-21 15:32:31.160: E/AndroidRuntime(2951): java.lang.OutOfMemoryError
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:374)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:404)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.Activity.performCreate(Activity.java:4465)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1092)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.os.Looper.loop(Looper.java:137)
02-21 15:32:31.160: E/AndroidRuntime(2951): at android.app.ActivityThread.main(ActivityThread.java:4447)
02-21 15:32:31.160: E/AndroidRuntime(2951): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 15:32:31.160: E/AndroidRuntime(2951): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 15:32:31.160: E/AndroidRuntime(2951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-21 15:32:31.160: E/AndroidRuntime(2951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-21 15:32:31.160: E/AndroidRuntime(2951): at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
啊 - 谷歌在Android中实现位图处理的许多精彩战斗.
每当我用内存进入屋顶时,我倾向于做一些内存分析 - 很多时候(尽管并非总是如此)这是因为一个人做错了事(即,意外的内存密集).只是浏览代码,我有点困惑的是需要强制代码通过三个流,只是为了再次结束位图.我觉得好像我错过了一些让我理解这一点的信息,但是如果我是你的话,我肯定会看看你在每个步骤中分配多少内存(堆转储 - > hprof -conv - >内存分析器是你的朋友).
但是,我怀疑你可能正在寻找的是BitmapRegionDecoder.我实际上最近才发现这一点,但我认为它确实完全符合您的要求 - 允许您从位图(输入流)解码区域而无需将整个位图加载到内存中(您也可以获得宽度/高度) .唯一的缺点是它只是Android 2.3.3+,但是现在这个市场> 90%的市场应该不是一个大问题.我已经完成了一些相当简洁的工作(平滑的平移和6000x4000图像的缩放) - 如果正确使用它肯定是内存有效.
[编辑]
我最近使用BitmapRegionDecoder打开了我自己的哑实现,它可以处理非常大的位图的平移,拖动和缩放.您可以在https://github.com/micabyte/android_game找到代码(ASL2.0)
您要查看的类是BitmapSurfaceRenderer.到目前为止,我已经用高达8000x4000的图像测试了它,它似乎运行得很好.当我有空的时候,我会提出一个简单的示例应用程序来演示如何使用它.
| 归档时间: |
|
| 查看次数: |
1605 次 |
| 最近记录: |