Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 设置ImageView时发生OutOfMemory错误_Android_Memory_Imageview_Resources_Drawable - Fatal编程技术网

Android 设置ImageView时发生OutOfMemory错误

Android 设置ImageView时发生OutOfMemory错误,android,memory,imageview,resources,drawable,Android,Memory,Imageview,Resources,Drawable,我正在尝试添加一些线性布局,每个布局在运行时都包含文本视图和图像视图 一切正常,但在尝试setImageResource时应用程序崩溃。(OutOfMemoryError) 以下是我的活动(相关部分): my_icon是可在res文件夹中绘制的名称(每个循环中的名称不同(名称来自xml) 有两种方法可以解决这个问题 1.尽量缩小图像大小[强烈建议] 您可以访问以缩小尺寸,同时保持图像质量 确保您没有使用非常高分辨率的图像作为 它们没有用处 2.使用大堆[不推荐] 在清单中添加largeHe

我正在尝试添加一些
线性布局
,每个布局在运行时都包含
文本视图
图像视图

一切正常,但在尝试
setImageResource
时应用程序崩溃。(OutOfMemoryError

以下是我的活动(相关部分):

my_icon
是可在res文件夹中绘制的名称(每个循环中的名称不同(名称来自xml)


有两种方法可以解决这个问题

1.尽量缩小图像大小[强烈建议]

  • 您可以访问以缩小尺寸,同时保持图像质量

  • 确保您没有使用非常高分辨率的图像作为 它们没有用处

2.使用大堆[不推荐]

  • 在清单中添加largeHeap=“true”

  • 这可能会稍微降低应用程序的性能


你可以在这里找到这个问题:解决了…图像太大了…只是缩小了尺寸。我不确定这种方法“getDrawableRessorceIdByName”,你可以使用这种方法“Resources.getIdentifier”,或者你可能已经在使用它了。它正在工作,请在开始之前阅读我的评论
// looping through all item nodes <item>
                    for (int i = 0; i < nl.getLength(); i++) {
                        Element e = (Element) nl.item(i);

                        String my_icon = parser.getValue(e, "my_icon"); // my_icon child value
                        int my_count = Integer.parseInt(parser.getValue(e, "my_count")); // my_count child value
                        String my_text = parser.getValue(e, "my_text"); // my_text child value

                        //Create Child-Views
                        LinearLayout tempLinearLayout = new LinearLayout(AnleitungActivity.this);
                        linearLayout.addView(tempLinearLayout);
                            tempLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
                            ViewGroup.LayoutParams params = tempLinearLayout.getLayoutParams();
                            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                            params.height = 240;
                            tempLinearLayout.setBackgroundColor(ContextCompat.getColor(AnleitungActivity.this, R.color.anleitung_kachel_hintergrundfarbe));

                            ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) tempLinearLayout.getLayoutParams();
                            marginParams.setMargins(0,5,0,0); //für die Margin-Werte brauchen wir extra MarginLayoutParams...
                            tempLinearLayout.setLayoutParams(params); //layoutParams setzen
                            tempLinearLayout.setLayoutParams(marginParams); //MarginLayoutParams setzen

                        TextView tv = new TextView(AnleitungActivity.this);
                        tempLinearLayout.addView(tv);
                            LinearLayout.LayoutParams tvParams  = new LinearLayout.LayoutParams(70, ViewGroup.LayoutParams.MATCH_PARENT,1f);
                            tv.setLayoutParams(tvParams);
                            tv.setText(transformText(my_text));
                            tv.setTextSize(17);
                            tv.setPadding(5,0,0,0);
                            tv.setTextColor(ContextCompat.getColor(AnleitungActivity.this, R.color.anleitung_text_farbe));

                        ImageView imageView = new ImageView(AnleitungActivity.this);
                        tempLinearLayout.addView(imageView);
                            imageView.getLayoutParams().height = dp2px(getResources(), 60);
                            imageView.getLayoutParams().width = dp2px(getResources(), 60);
                            imageView.setImageResource(getDrawableRessorceIdByName(AnleitungActivity.this, removeFileExtensionFromString(my_icon)));



                    }
    public String removeFileExtensionFromString(String str){
    return str.substring(0, str.indexOf("."));
}