gpt4 book ai didi

android - 在 (`Drawable` 的 android `bitmap` 中使用图像的有效方法)

转载 作者:行者123 更新时间:2023-11-29 00:47:55 25 4
gpt4 key购买 nike

图像总是给我带来麻烦:(。
java.lang.OutOfMemoryError: bitmap size exceeds VM budget - android 即使我使用非常小的图像也是主要问题。
请给我一些使用 Image 的提示。 (链接、教程、示例项目等...)

我的一些问题是

  1. 我应该选择 Drawable 还是 bitmap
  2. BitmapFactory.decodeFileDrawable.createFromPath
  3. BitmapFactory.decodeStreamDrawable.createFromStream
  4. 如何在旋转时保留DrawableBitmap
  5. 通过intent保存或传递DrawableBitmap是个好主意吗

最佳答案

这完全取决于您想要对图像做什么。位图工厂用于创建位图。当你想画东西的时候会用到 drawable objects :)

在内存中创建位图后,您想对位图做什么?

至于旋转,你应该注意onRestoreInstanceState和onSaveInstanceState。像这样:

private static final String PHOTO_TAKEN = "photo_taken";
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(PHOTO_TAKEN)) {
// do something if you have your pic here
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(PHOTO_TAKEN, mPhotoTaken); // saving the state of the image (if the photo is taken or not in this case)
}

至于通过附加信息...我敢打赌这是个坏主意,因为您已经遇到内存不足异常...您可以传递对该图像的引用,例如路径 ID 或其他内容。

更新:
在其中一个项目中,我试图在 ImageView 控件中显示从摄像头拍摄的图像我的 Activity ,并得到了内存不足的异常。原因是它将整个大图像放入内存中。解决方法非常简单:我减小了图像大小:

    BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;

Bitmap bitmap = BitmapFactory.decodeFile(mImagePath, options);

关于android - 在 (`Drawable` 的 android `bitmap` 中使用图像的有效方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5257616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com