简体   繁体   English

在自定义列表视图中添加图像时,Android应用崩溃

[英]Android-app crash on adding image in custom list view

i am making custom list view in which i have to add images taken by user either from sd card or by camera.For this i take one image view and two buttons.one button take picture from camera and other from sd card and then they add picture in image view..Now when i pass that image uri in custom list view then for the first time image is added but second time application crashes....if someone knows the answer then plz post it. 我正在制作自定义列表视图 ,其中我必须添加用户从sd卡或相机拍摄的图像。为此,我拍摄一个图像视图和两个按钮。一个按钮从相机拍摄照片,另一个从sd卡拍摄照片,然后添加现在,当我在自定义列表视图中传递该图像uri时,将首次添加图像,但第二次应用程序崩溃....如果有人知道答案,则请发布它。 thanks in advance!! 提前致谢!!

here is the code. 这是代码。 This is for button click.. 这是用于按钮单击。

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);

After this.. 在这之后..

@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();
                }
            }

        }

and then im adding image in list View 然后我在列表视图中添加图像

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;

Have you read this documentation ? 您阅读过此文档吗?

In particular you need to check out how to manage memory . 特别是,您需要检查如何管理内存 Before you load your bitmap, you should try to verify what options you need to use to downscale its size, for instance. 例如,在加载位图之前,您应该尝试验证需要使用哪些选项来缩小其尺寸。

This answer might help you with the code you need to re-scale your image before you try to load it. 此答案可能会帮助您提供在尝试加载图像之前需要重新缩放图像的代码。

Rather big image from the camera? 相机的大图像? Try decreasing the size of that image (reducing the resolution in the camera settings). 尝试减小该图像的尺寸(减小相机设置中的分辨率)。

Looks like you are exceeding the memory you have available to your app: 看起来您超出了应用程序可用的内存:

From the log you posted: 从您发布的日志中:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

But other than that you are probably leaking some memory. 但是除此之外,您可能正在泄漏一些内存。 You should discard (recycle) those bitmaps etc. 您应该丢弃(回收)那些位图等。

@DigCamara posted some excellent links in his/her answer! @DigCamara在他/她的答案中张贴了一些出色的链接! Really, a must read for your situation. 确实,您的情况必须阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM