【问题标题】:Displaying a lot of bitmaps in android app在android应用程序中显示很多位图
【发布时间】:2013-01-17 13:42:46
【问题描述】:

我正在尝试向我在网站上找到的查看器显示大量图片

我添加了这段代码,在 stackoverflow 上找到,但看起来还不够

                bmpOptions.inSampleSize = sampleSize;
                bmpOptions.inDither=false;                     //Disable Dithering mode
                bmpOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
                bmpOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
                bmpOptions.inTempStorage=new byte[32 * 1024];

我该如何改进它? 这是完整的代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        // check if we are returning from picture selection
        if (requestCode == PICKER) {
            // the returned picture URI
            Uri pickedUri = data.getData();
            // declare the bitmap
            Bitmap pic = null;
            // declare the path string
            String imgPath = "";
            // retrieve the string using media data
            String[] medData = { MediaStore.Images.Media.DATA };
            // query the data
            Cursor picCursor = managedQuery(pickedUri, medData, null, null,
                    null);
            if (picCursor != null) {
                // get the path string
                int index = picCursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                picCursor.moveToFirst();
                imgPath = picCursor.getString(index);
            } else
                imgPath = pickedUri.getPath();
            // if we have a new URI attempt to decode the image bitmap
            if (pickedUri != null) {
                // set the width and height we want to use as maximum
                // display
                int targetWidth = 600;
                int targetHeight = 400;
                // create bitmap options to calculate and use sample size
                BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
                // first decode image dimensions only - not the image bitmap
                // itself
                bmpOptions.inDither=false;                     //Disable Dithering mode
                bmpOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
                bmpOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
                bmpOptions.inTempStorage=new byte[32 * 1024];
                bmpOptions.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(imgPath, bmpOptions);
                // image width and height before sampling
                int currHeight = bmpOptions.outHeight;
                int currWidth = bmpOptions.outWidth;
                // variable to store new sample size
                int sampleSize = 1;
                // calculate the sample size if the existing size is larger
                // than target size
                if (currHeight > targetHeight || currWidth > targetWidth) {
                    // use either width or height
                    if (currWidth > currHeight)
                        sampleSize = Math.round((float) currHeight
                                / (float) targetHeight);
                    else
                        sampleSize = Math.round((float) currWidth
                                / (float) targetWidth);
                }
                // use the new sample size
                bmpOptions.inSampleSize = sampleSize;
                bmpOptions.inDither=false;                     //Disable Dithering mode
                bmpOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
                bmpOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
                bmpOptions.inTempStorage=new byte[32 * 1024];
                // now decode the bitmap using sample options
                bmpOptions.inJustDecodeBounds = false;
                // get the file as a bitmap
                pic = BitmapFactory.decodeFile(imgPath, bmpOptions);
                // pass bitmap to ImageAdapter to add to array
                imgAdapt.addPic(pic);
                // redraw the gallery thumbnails to reflect the new addition
                picGallery.setAdapter(imgAdapt);
                // display the newly selected image at larger size
                picView.setImageBitmap(pic);
                // scale options
                picView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                pic.recycle();
            }

        }
    }

【问题讨论】:

    标签: java android bitmap out-of-memory


    【解决方案1】:

    首先跟踪您的堆大小在 20 张图像后增加,然后 您可以尝试这个简单的解决方案 Just Pass Image 和 Width 和 height 将返回 Resized Image

    public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
        int width = image.getWidth();
        int height = image.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
                matrix, false);
        return resizedBitmap;
    }
    

    【讨论】:

      【解决方案2】:

      你必须在放置调整大小位图代码后缩放位图,或者你可以直接在图像视图上设置试试这个

      int h = 100; // height in pixels
      int w = 100; // width in pixels
      Bitmap photoBitMap = Bitmap.createScaledBitmap(yourSelectedImage, h, w, true);
      imageView.setBitmap(photoBitMap);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多