【问题标题】:Error is java.lang.OutOfMemoryError错误是 java.lang.OutOfMemoryError
【发布时间】:2016-08-02 10:17:11
【问题描述】:
        File imgFile = new File(path);

        if (imgFile.exists()) {
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inSampleSize = 2;
            options.inJustDecodeBounds = false;
            options.inTempStorage = new byte[16 * 1024];

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        //Drawable d = new BitmapDrawable(getResources(), myBitmap);
        float scale = context.getResources().getDisplayMetrics().density;
        holder.userprofile.setImageBitmap(myBitmap);
        holder.userprofile.setLayoutParams(new LinearLayout.LayoutParams((int) (350 * scale), (int) (300 * scale)));

        holder.userprofile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isImageFitToScreen) {
                    Toast.makeText(context, "NORMAL SIZE!", Toast.LENGTH_LONG).show();


                    float scale = context.getResources().getDisplayMetrics().density;
                    holder.userprofile.setLayoutParams(new LinearLayout.LayoutParams((int) (350 * scale), (int) (300 * scale)));
                    holder.userprofile.setAdjustViewBounds(true);


                    isImageFitToScreen = false;
                } else {
                    Toast.makeText(context, "FULLSCREEN!", Toast.LENGTH_LONG).show();
                    float scaleHeigth = context.getResources().getDisplayMetrics().heightPixels;
                    float scaleWidth = context.getResources().getDisplayMetrics().widthPixels;
                    holder.userprofile.setLayoutParams(new LinearLayout.LayoutParams((int) scaleWidth, (int) scaleHeigth));
                    holder.userprofile.setScaleType(ImageView.ScaleType.FIT_XY);


                    isImageFitToScreen = true;
                }

            }
        });

问题是由于内存错误,应用程序运行速度太慢错误是当单击图像上的按钮时,它将在全屏图像中看到,再次单击图像将再次变为原始图像。 这是错误:

java.lang.OutOfMemoryError: Failed to allocate a 63701004 byte allocation with 16777120 free bytes and 49MB until OOM

提前谢谢你

【问题讨论】:

  • 您的OOM发生在哪里?在 myBitmap 分配还是在 onClick 方法中?
  • myBitmap 分配@nshmura

标签: android out-of-memory android-bitmap bitmapimage bitmapfactory


【解决方案1】:

像这样使用optionsdecodeFile 方法:

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);

【讨论】:

    猜你喜欢
    • 2013-08-04
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多