简体   繁体   中英

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. 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! Really, a must read for your situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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