android加載大圖片到內存

jopen 11年前發布 | 64K 次閱讀 Android Android開發 移動開發

在andorid編程中,我們有時需要講一張像素很高的圖片加載的圖片中,如果我們是這樣去做

    public void load1(View view) {  
        String src = "mnt/sdcard/DSC.jpg";  
        Bitmap bitmap = BitmapFactory.decodeFile(src);  
        this.iv_img.setImageBitmap(bitmap);  
    }  
可能就是拋出java.lang.OutOfMemoryError異常

這是由于在android系統中,默認情況下,dailvike虛擬機只會為每個應用程序分配16M的內存空間

而加載高像素的圖片是非常消耗內存的,如果這時我們不將圖片進行縮放就直接加載到內存,很容易就會拋出內存泄漏的異常

因此,我們可以這么來操作

    public class MainActivity extends Activity {  
        private ImageView iv_img;  
        private int windowHeight;  
        private int windowWidth;  

        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            this.iv_img = (ImageView) this.findViewById(R.id.iv_img);  
            WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);  
            // 第一種獲取手機屏幕寬高的方法  
            this.windowHeight = manager.getDefaultDisplay().getHeight();  
            this.windowWidth = manager.getDefaultDisplay().getWidth();  
            System.out.println("手機寬 :" + this.windowWidth);  
            System.out.println("手機高 :" + this.windowHeight);  

            // 第二種獲取手機屏幕寬高的方法,但是getSize()是從 API Level 13才有的方法  
            // Point outSize = new Point();  
            // manager.getDefaultDisplay().getSize(outSize );  
            // this.windowWidth = outSize.x;  
            // this.windowHeight = outSize.y;  
        }  

        public void load1(View view) {  
            String src = "mnt/sdcard/DSC.jpg";  
            Bitmap bitmap = BitmapFactory.decodeFile(src);  
            this.iv_img.setImageBitmap(bitmap);  
        }  

        public void load2(View view) {  
            String src = "mnt/sdcard/DSC.jpg";  

            // 圖片解析的配置  
            Options options = new Options();  
            // 不去真正解析圖片,只是獲取圖片的寬高  
            options.inJustDecodeBounds = true;  
            BitmapFactory.decodeFile(src, options);  
            int imageWidth = options.outWidth;  
            int imageHeight = options.outHeight;  
            System.out.println("圖片寬 :" + imageWidth);  
            System.out.println("圖片高 :" + imageHeight);  

            int scaleX = imageWidth / this.windowWidth;  
            int scaleY = imageHeight / this.windowHeight;  
            int scale = 1;  
            if (scaleX >= scaleY && scaleX >= 1) {  
                // 水平方向的縮放比例比豎直方向的縮放比例大,同時圖片的寬要比手機屏幕要大,就按水平方向比例縮放  
                System.out.println("按寬比例縮放");  
                scale = scaleX;  
            } else if (scaleY >= scaleX && scaleY >= 1) {  
                // 豎直方向的縮放比例比水平方向的縮放比例大,同時圖片的高要比手機屏幕要大,就按豎直方向比例縮放  
                System.out.println("按高比例縮放");  
                scale = scaleY;  
            }  
            System.out.println("縮放比例:" + scale);  
            // 真正解析圖片  
            options.inJustDecodeBounds = false;  
            // 設置采樣率  
            options.inSampleSize = scale;  
            Bitmap bitmap = BitmapFactory.decodeFile(src,options);  
            this.iv_img.setImageBitmap(bitmap);  

        }  
    }  
簡單的布局文件
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:paddingBottom="@dimen/activity_vertical_margin"  
        android:paddingLeft="@dimen/activity_horizontal_margin"  
        android:paddingRight="@dimen/activity_horizontal_margin"  
        android:paddingTop="@dimen/activity_vertical_margin"  
        tools:context=".MainActivity" >  

        <Button  
            android:onClick="load1"  
            android:id="@+id/button1"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignParentLeft="true"  
            android:layout_alignParentTop="true"  
            android:text="不縮放加載圖片" />  

        <ImageView  
            android:id="@+id/iv_img"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:layout_alignLeft="@+id/button1"  
            android:layout_below="@+id/button1"  
            android:src="@drawable/ic_launcher" />  

        <Button  
            android:onClick="load2"  
            android:id="@+id/button2"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_alignBottom="@+id/button1"  
            android:layout_toRightOf="@+id/button1"  
            android:text="縮放加載圖片" />  

    </RelativeLayout>  
進過縮放之后的圖片再加載到內存,就不會再拋出異常。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!