位图加载:java.lang.OutofMemoryError:Java堆空间

时间:2015-12-07 11:13:12

标签: java android image bitmap

我正在使用Android ScrollViewGallery来显示我使用默认系统相机拍摄的图像。

在我的Android版Nexus 5上运行正常。 它在使用Android 4.3的虚拟三星Galaxy S4上也能正常工作 和Android 4.3的虚拟索尼Xperia Z.

但它不适用于Android 4.4的OnePlus One 以及三星Galaxy Note 3.

我遇到了问题:

  

java.lang.OutofMemoryError:Java堆空间

我不明白为什么它可以在某些设备上运行而在其他设备上则不然。

我的代码加载位图:

public class ImagesActivity extends FragmentActivity {
    static final String PUTEXTRA_IDENTIFIER_ANLAGE = "selectedAnlage";
    static final String PUTEXTRA_IDENTIFIER_MASCHINE = "selectedMaschine";
    public int selectedAnlage;
    public int selectedMaschine;

    private Toolbar mToolbar;
    private ScrollGalleryView mScrollGalleryView;
    private List<Image> imagesList;

    private File mediaStorageDir;

    private static final String IMAGE_DIRECTORY_NAME = "SMSImageDokumentation";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_images);

        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if(extras == null) {
                selectedAnlage = 0;
            } else {
                selectedAnlage = extras.getInt(PUTEXTRA_IDENTIFIER_ANLAGE);
            }
        } else {
            selectedAnlage= (int) savedInstanceState.getSerializable(PUTEXTRA_IDENTIFIER_ANLAGE);
        }

        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if(extras == null) {
                selectedMaschine = 0;
            } else {
                selectedMaschine = extras.getInt(PUTEXTRA_IDENTIFIER_MASCHINE);
            }
        } else {
            selectedMaschine= (int) savedInstanceState.getSerializable(PUTEXTRA_IDENTIFIER_MASCHINE);
        }


        DatabaseHandler db = new DatabaseHandler(this);
        imagesList = db.getImagesForKEyMaschineAndAnlage(selectedAnlage, selectedMaschine);

        mScrollGalleryView = (ScrollGalleryView)findViewById(R.id.images_scroll_gallery_view);
        mScrollGalleryView
                .setThumbnailSize(100)
                .setZoom(true)
                .setFragmentManager(getSupportFragmentManager());

        for(int i = 0; i <= imagesList.size() -1; i++)
        {
            try {
                mScrollGalleryView.addImage(MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(imagesList.get(i).get_fileName())));
            } catch (IOException e) {
                e.printStackTrace();

                Toast.makeText(this,
                        "Fehler beim Laden", Toast.LENGTH_SHORT)
                        .show();

            }
        }

    }
}

0 个答案:

没有答案