简体   繁体   中英

How to share image from SDcard to gmail in android/ ANDROID?

I am doing R&D in this topic.

I am getting image from gallery and able to view in image view.

And by long press over that image view i can able to share.

But the problem is i am not getting the attached image as output..

 public class Facebookhome extends Activity {

Button share;

ImageView img;

Uri screenshotUri;

private static final int SELECT_PICTURE = 1;

private String selectedImagePath;

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_facebookhome);

    share = (Button) findViewById(R.id.button1);

    img = (ImageView) findViewById(R.id.imageView1);

    share.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent intent = new Intent();

            intent.setType("image/*");

            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(

                    Intent.createChooser(intent, "Select 

 Picture"),

                    SELECT_PICTURE);

        }

    });


    img.setOnLongClickListener(new View.OnLongClickListener() {

        public boolean onLongClick(View v) {

            shareimage();

            return true;

        }

    });

}

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {

        if (requestCode == SELECT_PICTURE) {

            Uri selectedImageUri = data.getData();

            selectedImagePath = getPath(selectedImageUri);

            System.out.println("Image Path : " + selectedImagePath);

            img.setImageURI(selectedImageUri);

        }

    }

}

public String getPath(Uri uri) {

    String[] projection = { MediaStore.Images.Media.DATA };

    Cursor cursor = managedQuery(uri, projection, null, null, null);

    int column_index = cursor

            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

    cursor.moveToFirst();

    return cursor.getString(column_index);

}

public void shareitem() {

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);

    sharingIntent.setType("text/plain");

    String shareBody = "Here is the share content body";

    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,

            "Subject Here");

    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

    startActivity(Intent.createChooser(sharingIntent, "Share via"));

}

public void shareimage() {

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);

    screenshotUri = Uri.parse(selectedImagePath);

    sharingIntent.setType("image/jpg");

    sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,

            screenshotUri);

    startActivity(Intent.createChooser(sharingIntent, "Share image using"));

    // Toast.makeText(getBaseContext(), "FB Last",

    // Toast.LENGTH_LONG).show();

}
}
private static int RESULT_LOAD_IMAGE = 1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

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

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}


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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
       Uri screenshotUri = Uri.parse(picturePath);
        cursor.close();

        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("image/jpg"");
        i.putExtra(Intent.EXTRA_EMAIL,
                new String[] { "aa@gmail.com" });
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_STREAM, screenshotUri);
        startActivity(Intent.createChooser(i, "Send mail..."));


    }


}

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