在我的android应用程序中,我必须允许用户单击按钮打开图库并选择图像。然后需要将特定的选定图像加载到我的布局(UI)中的图像视图中。我有一些代码,但它来java.lang.outofmemory.Please任何人都可以帮助我?Android:应用程序在上传图像时在设备上崩溃,java.lang.outofMemoryError
1
A
回答
3
你应该解码在onActivityResult()方法中的图像URI。 将此方法调用decodeBitmap。
/**
* This is very useful to overcome Memory waring issue while selecting image
* from Gallery
*
* @param selectedImage
* @param context
* @return Bitmap
* @throws FileNotFoundException
*/
public static Bitmap decodeBitmap(Uri selectedImage, Context context)
throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(context.getContentResolver()
.openInputStream(selectedImage), null, o);
final int REQUIRED_SIZE = 100;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(context.getContentResolver()
.openInputStream(selectedImage), null, o2);
}
有关详情,请尽管话题显示位图高效
http://developer.android.com/training/displaying-bitmaps/index.html
希望这有助于。
+0
谢谢你很多..工作精细.. – shakthivel
0
在这里,你会发现为什么你这个例外,如何正确地显示位图:http://developer.android.com/training/displaying-bitmaps/index.html
相关问题
- 1. 应用程序在设备上崩溃
- 2. Android应用程序在设备上启动时崩溃
- 3. Android应用程序在Android 5设备上崩溃
- 4. Android地图应用在启动时在设备上崩溃
- 5. 应用程序崩溃只在设备上,但不在emulator上
- 6. Android - 应用程序在Pre-Lollipop设备上崩溃
- 7. Android应用程序只在设备上崩溃
- 8. 在某些设备上崩溃的Android应用程序
- 9. 应用程序在设备上运行时突然崩溃
- 10. 应用程序在设备上调试时崩溃
- 11. 加载图像时发生应用程序崩溃(java.lang.OutOfMemoryError)
- 12. Android应用程序在移动设备上崩溃,但在Eclipse上运行!
- 13. android应用程序在startActivity上崩溃()
- 14. Android应用程序在starup上崩溃
- 15. bada应用在设备上崩溃
- 16. 应用程序在设备上崩溃,而只使用仪器
- 17. 在真实设备上测试时Android应用崩溃
- 18. huawaei设备上的应用程序崩溃(Android 6.0 api 23)
- 19. 更新后应用程序在某些设备上崩溃
- 20. 在iOS设备上崩溃的应用程序
- 21. Winforms应用程序在手持设备上崩溃
- 22. 应用程序在3GS设备上崩溃
- 23. 我的应用程序在设备上崩溃
- 24. 应用程序在设备上的电子邮件崩溃
- 25. iPhone:应用程序在特定设备上崩溃
- 26. iOS应用程序在没有崩溃信息的设备上崩溃
- 27. AVAnimator在设备上崩溃
- 28. Digits.sharedInstance()在设备上崩溃
- 29. Android应用程序在设备上运行完美,但在模拟器崩溃
- 30. 应用程序适用于某些设备,但在其他设备上崩溃
您正在加载的位图可能对测试设备上可用的内存量太大。或者,您可能一次将太多位图加载到库中。人们经常碰到这种情况,在处理Android上的位图时,必须对代码进行一些预先考虑。这里是由罗曼盖伊介绍这个话题:https://dl.google.com/io/2009/pres/Th_0230_TurboChargeYourUI-HowtomakeyourAndroidUIfastandefficient.pdf – Turnsole
@Tnsnsole谢谢你的工作很好 – shakthivel