【问题标题】:Put a big image in an ImageView not working将大图像放入 ImageView 不起作用
【发布时间】:2011-11-20 21:35:25
【问题描述】:

我认为我的 ImageView 有问题。 我创建了一个画廊,我可以在其中触摸图像并将其放在下面的 ImageView 中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:background="@drawable/fonddegrade">

<Gallery android:layout_height="wrap_content" 
    android:id="@+id/gallery" 
    android:layout_width="fill_parent" />

<ImageView android:layout_below="@+id/gallery"
    android:layout_height="wrap_content" 
    android:id="@+id/laphoto" 
    android:layout_width="wrap_content" 
    android:layout_centerHorizontal="true"/>

这适用于小图像,但不适用于大图像 (3264 * 1952)。当我触摸它时(因此,试图将它放在 ImageView 中),我有一个错误并且我的应用程序崩溃了。 这是我显示图像的 java 代码:

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.photo);

            File images; // Trouver le bon endroit de stockage
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
                images = new File("/sdcard/MyPerformance/Photo");
            else
                images = this.getFilesDir();

            images.mkdirs();
            File[] imagelist = images.listFiles(new FilenameFilter(){   
                @Override   
                public boolean accept(File dir, String name)   
                {   
                    return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
                }
            });

            mFiles = new String[imagelist.length];
            for(int i = 0 ; i< imagelist.length; i++)   
            {   
                mFiles[i] = imagelist[i].getAbsolutePath();   
            }
            mUrls = new Uri[mFiles.length];
            for(int i = 0; i < mFiles.length; i++)   
            {   
                mUrls[i] = Uri.parse(mFiles[i]);      
            }

            imgView = (ImageView)findViewById(R.id.laphoto);
            if(mFiles.length != 0)
                imgView.setImageURI(mUrls[0]);

            gallery = (Gallery) findViewById(R.id.gallery);
            gallery.setAdapter(new ImageAdapter(this));

            gallery.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    imgView.setImageURI(mUrls[position]);
                }
            });
    }

问题来自于 setImageURI(但我不认为这是原因,因为它适用于小图像)或图片的大小。

你能给我什么解决方案来解决这个问题? 谢谢你。

PS:为什么我的“你好”总是被删除?

【问题讨论】:

标签: android imageview gallery uri


【解决方案1】:

您的图片对于 Android 来说可能太大,并且内存不足。应用程序的可用内存可能低至 16Mb。您的图像需要 3264 * 1952 * 4 = ~25.5Mb(宽度高度argb)。因此,最好将图像调整为更小的尺寸。

见:http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html
然后:Strange out of memory issue while loading an image to a Bitmap object
最后:VM running out of memory while getting images from the cache

【讨论】:

  • 就是这样!您的链接对我帮助很大,我的问题与第二个链接相同,我使用相同的解决方案来解决该问题。因此,我们必须缩放太大的图像才能正常工作。
【解决方案2】:

这是一个位图[实用程序类,可帮助您处理大型位图。

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BitmapUtils {

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

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

    }

    return inSampleSize;
}


    public static Bitmap decodeSampledBitmapFromResource(String pathToFile,
                int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathToFile, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(pathToFile, options);
    }

}

【讨论】:

    【解决方案3】:

    Glide library:

    Glide.with(getContext())
         .load(selectedImageUri)
         .into(imageView);
    

    Glide 完成所有后端工作。

    【讨论】:

    • 虽然此链接可能会回答问题,但 Stack Overflow 上不鼓励仅链接的答案,您可以通过获取链接的重要部分并将其放入您的答案来改进此答案,这样可以确保您的答案是如果链接被更改或删除,仍然是一个答案:)
    猜你喜欢
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多