Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

I have an error

package com.example.socialneworkshortcut;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {
    Button post, buttonimage, postimage;
    EditText text;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        post = (Button) findViewById(R.id.button1);
        buttonimage = (Button) findViewById(R.id.button2);
        postimage = (Button) findViewById(R.id.button3);
        text = (EditText) findViewById(R.id.editText1);
        image = (ImageView) findViewById(R.id.imageView1);

        buttonimage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                        "Where do you want your photo from?"), 1);
            }
        });

        post.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_TEXT, text.getText()
                        .toString());

                startActivity(Intent.createChooser(shareIntent,
                        "Where do you want to share?"));
            }
        });

        postimage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.setType("image/jpeg");
                shareIntent.putExtra(Intent.EXTRA_STREAM, image);
                startActivity(Intent.createChooser(shareIntent, "Where do you want to share?"));
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                image.setImageURI(data.getData());
            }

        }
    }
}

shareIntent.putExtra(Intent.EXTRA_STREAM, image);

This line gives me and error which says "The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, ImageView)"

What am I doing wrong?

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Your image variable has the data type of ImageView. The ImageView is just the container to display the image--it's a layout element. It's not the kind of thing you want to share via the intent. I'm assuming you want to share the image itself, right? Totally understandable mistake. :) If you change the name of your image variable to imageView instead, then the naming convention might help you keep them separate.

You're setting the ImageView with the following line:

image.setImageURI(data.getData());

To share an image via an intent like you're doing with EXTRA_STREAM, you need to provide the URI to the image. So change that line to the following and I think you'll be set:

shareIntent.putExtra(Intent.EXTRA_STREAM, data.getData());

postimage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.setType("image/jpeg");
                shareIntent.putExtra(Intent.EXTRA_STREAM, data.getData());
                startActivity(Intent.createChooser(shareIntent, "Where do you want to share?"));
            }
        });

Did you mean like this because if this still doesn't work