簡體   English   中英

android - 使用Media.getBitmap的outofmemeory

[英]android - outofmemeory by using Media.getBitmap

我正在使用相機和畫廊。 我有代碼從畫廊獲取圖像並返回onactivityresult。

當用戶從圖庫中選擇圖像並轉到活動結果時,我會在此行中出現以下錯誤:

thePic = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());

我在哪里做錯了?

原因:當您的應用超過分配的堆內存時會發生這種情況。 圖像可能太大而無法放入堆中。在處理圖像之前,您必須縮小位圖。 請通過此鏈接進行正確修復以避免內存泄漏

快速修復:

增加應用的堆大小。 通過,將以下行放入manifest.xml文件中。

<application
     android:largeHeap="true" >

注意:這不是最佳做法。

查看可以幫助您的文檔 - 有效地加載大位圖

直接從gallary加載圖像可以殺死應用程序。 我們永遠不應該這樣做我們應該首先根據我們的要求縮放該圖像,並且應該使用該縮放圖像有時候應用程序無法在memmory中處理大位圖

因此,下面的代碼將解碼您所需大小的圖像只需將sizeOfImage提供給您需要的大小它還將管理您的圖像的arspect比率

將以下代碼放在您的onactivityresult中

Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(
        selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();

Bitmap mm = decodeSampledBitmapFromResource(new File(filePath),sizeOfImage,sizeOfImage);

上面的代碼將使用下面的函數,所以也把功能放在某處

    public static Bitmap decodeSampledBitmapFromResource(File res, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(res.getAbsolutePath(),options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(res.getAbsolutePath(),options);
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height;
            final int halfWidth = width;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

您應該根據您的要求更改此功能,並參考以下鏈接https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

在android中加載位圖的最佳做法是在加載之前找到位圖尺寸。 然后加載適當大小的位圖取決於您的設備內存大小和屏幕大小。

我強烈建議你在develope.android網站上閱讀這個很棒的教程。 這將有助於您全面了解位圖的工作原理,並了解加載它們的最佳實踐,還包括源代碼。 請仔細閱讀所有這些教程。

https://developer.android.com/training/displaying-bitmaps/index.html

https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

https://developer.android.com/training/displaying-bitmaps/process-bitmap.html

https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

https://developer.android.com/training/displaying-bitmaps/manage-memory.html

https://developer.android.com/training/displaying-bitmaps/display-bitmap.html

我建議你閱讀這些鏈接,但如果你還感興趣,你可以使用Glide或Picasso等庫來加載你的位圖。 它們將幫助您在沒有OutOfMemory錯誤的情況下加載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM