Android-app在自定义列表视图中添加图片时崩溃

时间:2013-04-19 18:05:14

标签: android android-listview camera out-of-memory

我正在制作自定义列表视图,其中我必须添加用户从SD卡或相机拍摄的图像。为此我拍摄一个图像视图和两个按钮。一个按钮拍摄照片相机以及 SD卡中的其他内容,然后他们在图片视图中添加图片。现在,当我在自定义列表视图中传递该图片时,第一次添加图片但是第二次应用程序崩溃....如果有人知道答案然后plz发布它。 提前谢谢!!

这是代码。 这是按钮点击..

camera.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
                        Environment.getExternalStorageDirectory(), "temp.jpg")));
                startActivityForResult(intent, 2);

            }
        });

        sdcard.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

                startActivityForResult(i, 0);

            }
        });

        adapter = new SimpleAdapter(
                this,
                list12,
                R.layout.list_row_1,
                new String[] { "name", "address", "location", "image" },
                new int[] { R.id.name, R.id.address, R.id.location, R.id.image });

        setListAdapter(adapter);

在此之后..

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 2) {
            File picture = new File(Environment.getExternalStorageDirectory()
                    + "/temp.jpg");

            imgUri = Uri.fromFile(picture);
            targetImage.setImageURI(imgUri);
            location1 = "Camera";
        }

        else {
            if (resultCode == RESULT_OK) {
                targetUri = data.getData();
                textTargetUri.setText(targetUri.toString());
                try {
                    bitmap = BitmapFactory.decodeStream(getContentResolver()
                            .openInputStream(targetUri));

                    //decodeBitmap(bitmap, 4, 4);

                    targetImage.setImageBitmap(bitmap);
                    location1 = "Sd card";

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

然后我在列表视图中添加图像

private void populateList() {

    HashMap<String, Object> temp11 = new HashMap<String, Object>();
    name1 = textname.getText().toString();
    address1 = textaddress.getText().toString();


    if (name1.equals("") || address1.equals("")) {
        String str = "Don't Leave field blank !";
        Toast toast = Toast.makeText(LastPage.this,

        str, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    else if (location1 == "Sd card") {

        textname.setText("");
        textaddress.setText("");
        temp11.put("name", name1);
        temp11.put("address", address1);

        temp11.put("location", location1);

        temp11.put("image", targetUri);
        targetImage.setImageBitmap(null);
        list12.add(0, temp11);
        adapter.notifyDataSetChanged();
    } else if (location1 == "Camera") {

        textname.setText("");
        textaddress.setText("");
        temp11.put("name", name1);
        temp11.put("address", address1);
        temp11.put("location", location1);

        temp11.put("image", imgUri);
        targetImage.setImageBitmap(null);
        list12.add(0, temp11);
        adapter.notifyDataSetChanged();
    }

logcat的

04-20 00:03:49.599: E/AndroidRuntime(4129): FATAL EXCEPTION: main
04-20 00:03:49.599: E/AndroidRuntime(4129): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:573)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.ImageView.resolveUri(ImageView.java:521)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.ImageView.setImageURI(ImageView.java:305)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.SimpleAdapter.setViewImage(SimpleAdapter.java:264)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:192)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.AbsListView.obtainView(AbsListView.java:1610)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1264)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.ListView.onMeasure(ListView.java:1175)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.view.View.measure(View.java:8313)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.view.View.measure(View.java:8313)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.view.View.measure(View.java:8313)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.view.View.measure(View.java:8313)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.os.Looper.loop(Looper.java:130)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at android.app.ActivityThread.main(ActivityThread.java:3687)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at java.lang.reflect.Method.invoke(Method.java:507)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-20 00:03:49.599: E/AndroidRuntime(4129):     at dalvik.system.NativeStart.main(Native Method)
04-20 00:03:58.999: W/dalvikvm(4206): VFY: unable to resolve virtual method 2385: Lcom/korovyansk/android/sample/slideout/SampleActivity;.getActionBar ()Landroid/app/ActionBar;

2 个答案:

答案 0 :(得分:2)

您是否阅读过this documentation

特别需要查看manage memory的方法。在加载位图之前,您应该尝试验证需要使用哪些选项来缩小其大小,例如。

在尝试加载图片之前,

This answer可能会帮助您重新扩展图片所需的代码。

答案 1 :(得分:1)

来自相机的相当大的图像?尝试减小图像的大小(降低相机设置的分辨率)。

看起来你超出了你的应用程序可用的内存:

从您发布的日志中

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

但除此之外,你可能会泄漏一些记忆。你应该丢弃(回收)那些位图等。

@DigCamara在他/她的回答中发布了一些优秀的链接!真的,必须阅读你的情况。