Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Android Tutorials - สอนเขียน Android App ฟรี เขียนโปรแกรมแอนดรอยด์บน SmartPhone / Tablets > Android java.lang.OutOfMemoryError (แก้ปัญหาเรื่อง OutOfMemoryError ในการแสดงรูปภาพ)



Clound SSD Virtual Server

Android java.lang.OutOfMemoryError (แก้ปัญหาเรื่อง OutOfMemoryError ในการแสดงรูปภาพ)

Android java.lang.OutOfMemoryError (แก้ปัญหาเรื่อง OutOfMemoryError ในการแสดงรูปภาพ) ในการเขียนโปรแกรม Android เพื่อแสดงรูปภาพ ปัญหาที่หลาย ๆ คนพบเจอก็มือเมื่อโหลดหรือแสดงรูปภาพขนาดใหญ่บน ImageView เราจะพับปัญหาโปรแกรมหยุดการทำงานและแสดง Error ว่า java.lang.OutOfMemoryError ปัญหานี้เกิดจาก Android ได้ใช้ Memory ที่มีอยู่อย่างจำกัดกับการจัดการกับรุปภาพจนไม่เหลือ Memory ให้ทำงานอย่างอื่น และวิธีการแก้ปัยหาง่าย ๆ ก็คือ ใช้การย่อรุปภาพให้มีขนาดเล็กลง ก่อนที่จะนำไปแสดงผลกับ ImageView ด้วย Code ง่าย ๆ ดังนี้

ฟิังก์ชั่นการ Decode เพื่อย่อขนาด
	 public static Bitmap decodeFile(File file, int iWidth, int iHeight){
		 try {
		     //Decode image size
		     BitmapFactory.Options o = new BitmapFactory.Options();
		     o.inJustDecodeBounds = true;
		     BitmapFactory.decodeStream(new FileInputStream(file),null,o);

		     //The new size we want to scale to
		     final int REQUIRED_WIDTH = iWidth;
		     final int REQUIRED_HIGHT = iHeight;
		     //Find the correct scale value. It should be the power of 2.
		     int scale=1;
		     while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
		         scale*=2;

		     //Decode with inSampleSize
		     BitmapFactory.Options o2 = new BitmapFactory.Options();
		     o2.inSampleSize=scale;
		     return BitmapFactory.decodeStream(new FileInputStream(file), null, o2);
		 } catch (FileNotFoundException e) {}
		 return null;
	}

การใช้งาน
        Bitmap bm = decodeFile(new File("/sdcard/image.jpg"),200,200);
        imgView.setImageBitmap(bm);


อีกตัวอย่างจะย่อขนาดของรุปภาพให้เท่ากันขนาดของ ImageView
	 public static void decodeFile(ImageView imgView, String filePath){
		int targetW = imgView.getWidth();
		int targetH = imgView.getHeight();
		
		// Get the dimensions of the bitmap
		BitmapFactory.Options bmOptions = new BitmapFactory.Options();
		bmOptions.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filePath, bmOptions);
		int photoW = bmOptions.outWidth;
		int photoH = bmOptions.outHeight;

		// Determine how much to scale down the image
		int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

		// Decode the image file into a Bitmap sized to fill the View
		bmOptions.inJustDecodeBounds = false;
		bmOptions.inSampleSize = scaleFactor;
		bmOptions.inPurgeable = true;

		Bitmap bitmap = BitmapFactory.decodeFile(filePath, bmOptions);
		imgView.setImageBitmap(bitmap);
	}

	    ImageView imgView = (ImageView) findViewById(R.id.imgView);
	    decodeFile(imgView,"/sdcard/image.jpg");


อ่านเพิ่มเติม : Android Taking Photo and Resize Images (Quality, Size, Width, Height)







   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2015-11-16 15:56:49 / 2015-11-16 21:34:02
  Download : No files
 Sponsored Links / Related

 
Android Images & Media Widgets
Rating :

 
Android Background Color and Background Image
Rating :

 
Android Image Resource from URL Website
Rating :

 
Android Thumbnail from Gallery
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่