![]() The code is in the Hour21LargeImage project in MainActivity.java. We’ll step through the approach and the code. Listing 21.3 shows the code to address handling large images. The dimensions are reduced by a factor of 4. Given an image of 1000×400, an inSampleSize of 4 will result in a bitmap of 250×100. inSampleSize: This integer indicates how much the dimensions of the bitmap should be reduced.outHeight: The height of the image set when you use inJustDecodeBounds.outWidth: The width of the image set when you use inJustDecodeBounds.This is the key to getting the bitmap dimensions without the memory overhead of creating the bitmap. inJustDecodeBounds: If set to true, this option indicates that the bitmap dimensions should be determined by the BitmapFactory, but that the bitmap itself should not be created.You use the following options from the BitmapFactoryOptions class: ![]() ![]() You use the BitmapFactory.Options class with the BitmapFactory class it is essential to how you handle large Bitmaps. If you have a 20-inch image and a 4-inch display, you request that the bitmap that is created in memory be created at a size and resolution that is appropriate for the 4-inch display. After you have the bitmap, you can determine an appropriate size for the bitmap that will fit in your display. Getting those dimensions is not a memory-intensive activity. The goal is to get the dimensions of the underlying bitmap without actually rendering it. In all cases, working with appropriately sized images would be ideal, but that does not always happen. Some work is required to fix this, but handling an image this large is possible. This line of code causes the app to fail: tImageResource(R.drawable.largeimage) You can cause the app to fail with an out-of-memory error by trying to set an ImageView to this resource. The image is in the drawable resource folder and has the id R.drawable.largeimage. This example uses a photo that is 72 inches × 54 inches and that has a 28Mb file size. This example shows how to handle a single large image.Īs a demonstration, start with an image and increase it to an unrealistic size. The details of your app will influence your memory usage and the techniques that will work best in your case. You can manipulate the image to display well and save memory. Similarly, no point exists in showing a 20-inch image on a 3-inch phone screen. No point exists in showing a 10-foot mural in a 6-inch frame. You want to display the image at an appropriate size and resolution for the device. You’ll learn to fix the memory error for this case. When the image is just loaded into the ImageView, the app fails with an out-of-memory error. To demonstrate this, in this section, you create an unrealistically large image to display in an ImageView. Large images can have a significant impact on memory use in your app. There are techniques for avoiding the dreaded OOM (out of memory) exception. To create a bitmap from a file or InputStream, you use the decodeFile() and decodeStream() methods, respectively. To create a bitmap from a resource, you use the BitmapFactory method decodeResource(): Bitmap bitmap = codeResource(getResources(), R.drawable.someImage) ![]() Using a BitmapFactory, you can create bitmaps in three common ways: from a resource, a file, or an InputStream. You create bitmaps via the BitmapFactory () class. The Bitmap () class represents a bitmap image. Android Application Development in 24 Hours, Sams Teach Yourself, 3rd Edition
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |