gpt4 book ai didi

android - imageview android 内存不足

转载 作者:行者123 更新时间:2023-11-29 01:39:40 25 4
gpt4 key购买 nike

早上好,我尝试在我的 Android 应用程序中滚动大图像(800*3782,1.7 Mb png 文件)我使用了此文档 https://developer.android.com/training/displaying-bitmaps/load-bitmap.html但每次我都有同样的错误:

08-28 11:00:36.534: E/AndroidRuntime(2143): Caused by: java.lang.OutOfMemoryError
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.content.res.Resources.loadDrawable(Resources.java:1965)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.widget.ImageView.<init>(ImageView.java:120)
08-28 11:00:36.534: E/AndroidRuntime(2143): at android.widget.ImageView.<init>(ImageView.java:110)
08-28 11:00:36.534: E/AndroidRuntime(2143): ... 28 more

这是孔代码: 关于.java

public class About extends Activity {

ImageView mImageView; //reference to the ImageView
int xDim, yDim; //stores ImageView dimensions

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
//attach an instance of HandleClick to the Button

//reference the ImageView
mImageView=(ImageView)findViewById(R.id.imageView1);
mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.aboutscroll, 800, 1200));
}


@Override
//Get the size of the Image view after the
//Activity has completely loaded
public void onWindowFocusChanged(boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
xDim=mImageView.getWidth();
yDim=mImageView.getHeight();
}

//Load a bitmap from a resource with a target size
static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

//Given the bitmap size and View size calculate a subsampling size (powers of 2)
static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) {
int inSampleSize = 1; //Default subsampling size
// See if image raw height and width is bigger than that of required view
if (options.outHeight > reqHeight || options.outWidth > reqWidth) {
//bigger
final int halfHeight = options.outHeight / 2;
final int halfWidth = options.outWidth / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}

}

这是 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
</ScrollView>

</LinearLayout>

只有当我将图像缩放到 100*100 px 时它才有效女巫不是我的目标我想滚动所有图像而不缩放我在 nexus 7 模拟器中测试应用程序我希望有人帮助我并认为你

最佳答案

您可以如下增加分区大小

-partition-size 1024

在此处查看已接受的答案以获取更多详细信息

How to increase storage for Android Emulator? (INSTALL_FAILED_INSUFFICIENT_STORAGE)

此外,您是否尝试在设备上对其进行测试?

关于android - imageview android 内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25547321/

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