Android loads large images to memory,

Source: Internet
Author: User

Android loads large images to memory,

This article demonstrates how to load images to memory in android

First, design the interface:

The Code is as follows:

<LinearLayout 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: orientation = "vertical" tools: context = ". mainActivity "> <Button android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: onClick =" load "android: text = "loading images to memory"/> <ImageView android: id = "@ + id/iv" android: layout_width = "fill_parent" android: layout_height = "fill_parent"/> </LinearLayout>

Upload test images to mnt/sdcard

Add the logic code:

public class MainActivity extends Activity {    private ImageView iv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        iv = (ImageView) findViewById(R.id.iv);    }    public void load() {        Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/a.jpg");        iv.setImageBitmap(bitmap);    }}

Run the code to generate an error because the image is too large (select a large photo)

You can use static Options in BitmapFactory to obtain the image width and height without parsing the image information:

BitmapFactory. options opts = new Options (); // do not parse the image information, just get the width and height of the image header opts. inJustDecodeBounds = true; BitmapFactory. decodeFile ("/sdcard/a.jpg", opts); int imageHeight = opts. outHeight; int imagewidth = opts. outWidth; System. out. println ("Image Width:" + imagewidth); System. out. println ("Image Height" + imageHeight );

 

Run:

09-04 06:09:10. 519: I/System. out (1812): Image Width: 2560
09-04 06:09:10. 519: I/System. out (1812): The image height is 1920.

The following figure shows the screen width and height of the mobile phone:

// Obtain the WindowManager wm = (WindowManager) getSystemService (WINDOW_SERVICE); int height = wm. getdefadisplay display (). getHeight (); int width = wm. getdefadisplay display (). getWidth ();

We can see that the getHeight and getWidth methods are outdated. Use the following method instead:

// Obtain the WindowManager wm = (WindowManager) getSystemService (WINDOW_SERVICE); Point outSize = new Point (); wm. getdefadisplay display (). getSize (outSize); // Versions later than 3.0 can be used

The complete code is as follows:

Package com. wuyudong. loadimage; import android. OS. bundle; import android. annotation. suppressLint; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. bitmapFactory. options; import android. graphics. point; import android. view. menu; import android. view. view; import android. view. windowManager; import android. widget. imageView; public class MainActivity extends Activity {private ImageView iv; private int running wheight; private int running wwidth; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); iv = (ImageView) findViewById (R. id. iv); // obtain the width and height of the mobile phone screen. WindowManager wm = (WindowManager) getSystemService (WINDOW_SERVICE); then wheight = wm. getdefadisplay display (). getHeight (); expires wwidth = wm. getdefadisplay display (). getWidth (); // Point outSize = new Point (); // wm. getdefadisplay display (). getSize (outSize); // Versions later than 3.0 can be used // optional wheight = outSize. y; // required wwidth = outSize. x;} public void load (View view) {// Bitmap bitmap = BitmapFactory. decodeFile ("/sdcard/a.jpg"); // iv. setImageBitmap (bitmap); // configure BitmapFactory for Image Parsing. options opts = new Options (); // do not parse the image information, just get the width and height of the image header opts. inJustDecodeBounds = true; BitmapFactory. decodeFile ("/sdcard/a.jpg", opts); int imageHeight = opts. outHeight; int imagewidth = opts. outWidth; System. out. println ("Image Width:" + imagewidth); System. out. println ("Image Height" + imageHeight); // calculate the zoom ratio of int scaleX = imagewidth/expires wwidth; int scaleY = imageHeight/rotate wheight; int scale = 1; if (scaleX> scaleY & scaleY> = 1) {scale = scaleX;} if (scaleY> scaleX & scaleX> = 1) {scale = scaleY;} // parse image opts. inJustDecodeBounds = false; // sampling rate opts. inSampleSize = scale; Bitmap bitmap = BitmapFactory. decodeFile ("/sdcard/a.jpg", opts); iv. setImageBitmap (bitmap );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.