如何在图像视图中设置大图像

时间:2012-09-27 04:46:07

标签: android

我有一个图像视图,我想在其上设置图像。图像的大小为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>

3 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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现在好了 快乐

答案 2 :(得分:0)

请参阅Loading Large Bitmaps Effectively

您可以创建一个函数来计算降尺度因子:

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