1

我有一个图像视图,我想在它上面设置一个图像。图像大小为 7,707,446 字节。每当我尝试设置布局时,应用程序崩溃并且错误是内存不足错误。任何人都可以建议我如何解决它。xml 是:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:scrollbars="none">

    <RelativeLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/image" >
    </RelativeLayout>
</ScrollView>

4

3 回答 3

1
Bitmap picture=BitmapFactory.decodeFile("/sdcard...");
    int width = picture.getWidth();
    int height = picture.getWidth();
    float aspectRatio = (float) width / (float) height;
    int newWidth = 70;
    int newHeight = (int) (70 / aspectRatio);       
    picture= Bitmap.createScaledBitmap(picture, newWidth, newHeight, true);


public static Bitmap decodeImage(String arrayList_image) {

        URL aURL;

        try {

            aURL = new URL(arrayList_image);

            URLConnection conn = aURL.openConnection();

            conn.connect();

            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);

            bm = BitmapFactory.decodeStream(bis);

            bis.close();

            is.close();

            return bm;

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();
        }
        return null;
    }
于 2012-09-27T10:06:02.870 回答
0

嗨,我建议您尝试一下并根据设备显示您的图像。

Bitmap bmp = BitmapFactory.decodeResource(myContext.getResources(),drawableId);
            int w = bmp.getWidth();
            int h = bmp.getHeight();

            System.out.println("OrgWidth : " + w);
            System.out.println("orgHeight : " + h);

            int targetWidth = (screenWidth * w)/iPadWidth;
            int targetHeight = (screenHeight * h)/iPadHeight;

            System.out.println("TargetWidth : " + targetWidth);
            System.out.println("TargetHeight : " + targetHeight);

            int[] arrWidthHeight = {targetWidth,targetHeight};

            // This for Slove the out of Memory problem

            int newWidth = 110;
            int newHeight = 110;
            float scaleWidth = ((float) newWidth) / w;
            float scaleHeight = ((float) newHeight) / h;

            Matrix bMatrix = new Matrix();
            bMatrix.postScale(scaleWidth, scaleHeight);
            Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, w, h , bMatrix, true);

首先从资源中获取图像并根据设备找到图像的新高度和宽度,然后缩放此图像以解决这里内存不足的问题。

这用于根据设备高度和宽度查找高度和宽度..

WindowManager winManager = (WindowManager) myContext.getSystemService(Context.WINDOW_SERVICE);
            screenWidth = winManager.getDefaultDisplay().getWidth();
            screenHeight = winManager.getDefaultDisplay().getHeight();

            System.out.println("Screen Width : " + screenWidth);
            System.out.println("Screen Height : " + screenHeight);

并使用位图来获取图像的高度和宽度,例如 ipathwidth 和 ipathHeight 现在好了

于 2012-09-27T04:51:19.043 回答
0

请参阅有效加载大型位图

您可以创建一个函数来计算缩小因子:

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) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
}

然后用它来加载一个缩放的位图:

public 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);
}

因此,如果您想将 100x100 像素的缩略图加载到 ImageView 中,您只需使用:

mImageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100)
);
于 2012-09-27T05:00:34.303 回答