使用BitmapFactory.decodeStream()

时间:2017-08-05 18:29:46

标签: android bitmap out-of-memory

我正在尝试从文件中获取位图,我得到的是内存异常,这里是代码,我已经尝试了一切--bitmap.recycle,bitmap == null,在清单中将largeHeap设置为true - 但错误还是会发生错误发生在位图= BitmapFactory.decodeStream(是); 解决方案应该从jelly bean android版本到最新版本。这是代码:

mMemoryStorage.mRetriever = new MediaMetadataRetriever();
try {
    for (int i = 0; i < mMemoryStorage.AllsongsArray.size(); i++) {
        FileInputStream inputStream = new FileInputStream(mMemoryStorage.AllsongsArray.get(i).mPath);
        mMemoryStorage.mRetriever.setDataSource(inputStream.getFD());
        inputStream.close();

        byte[] art = mMemoryStorage.mRetriever.getEmbeddedPicture();
        Bitmap bitmap = null;
        if (art != null) {

            InputStream is = new ByteArrayInputStream(art);
            *Error//////bitmap = BitmapFactory.decodeStream(is);////////////Error*

            final int maxSize = 512;
            int outWidth;
            int outHeight;
            int inWidth = bitmap.getWidth();
            int inHeight = bitmap.getHeight();
            if (inWidth > inHeight) {
                outWidth = maxSize;
                outHeight = (inHeight * maxSize) / inWidth;
            }
            else {
                outHeight = maxSize;
                outWidth = (inWidth * maxSize) / inHeight;
            }
            //CAUSES OUT OF MEMORY ERROR
            Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, outWidth, outHeight, false);
            bitmap.recycle();
            bitmap = null;
            mMemoryStorage.AllsongsArray.get(i).mBitmapEmbedded = resizedBitmap;
        }
    }
} catch (Exception ex) {
    ex.printStackTrace();
}
mMemoryStorage.mRetriever.release();

0 个答案:

没有答案