1

Following is my code for selecting pictures and sending them via email but i am not able to select and send any photo from SD card and internal storage. I am totally new to android and not able to understand suggestions already available on internet. Please provide the code changes required below and also how can i send phone number via email that has been taken from user through app??

MainActivity.java

package com.example.android.send_email_app;

import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView edittextEmailAddress;
    EditText edittextEmailSubject;
    EditText edittextEmailText;
    ImageView btnAddFile, btnSend;
    ListView listViewFiles;

    ArrayList<Uri> arrayUri = new ArrayList<Uri>();
    ArrayAdapter<Uri> myFileListAdapter;

    final int RQS_LOADIMAGE = 0;
    final int RQS_SENDEMAIL = 1;

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

        edittextEmailAddress = (TextView) findViewById(R.id.email_address);
        edittextEmailSubject = (EditText)findViewById(R.id.email_subject);
        edittextEmailText = (EditText)findViewById(R.id.email_text);
        edittextEmailAddress.setText("abc@gmail.com");
        btnAddFile = (ImageView) findViewById(R.id.attachment);
        btnSend = (ImageView) findViewById(R.id.send);
        btnAddFile.setOnClickListener(btnAddFileOnClickListener);
        btnSend.setOnClickListener(btnSendOnClickListener);

        myFileListAdapter = new ArrayAdapter<Uri>(
                MainActivity.this,
                android.R.layout.simple_list_item_1,
                arrayUri);
        listViewFiles = (ListView)findViewById(R.id.filelist);
        listViewFiles.setAdapter(myFileListAdapter);
    }

    OnClickListener btnAddFileOnClickListener
            = new OnClickListener(){

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, RQS_LOADIMAGE);

        }};

    OnClickListener btnSendOnClickListener
            = new OnClickListener(){

        @Override
        public void onClick(View v) {
            String emailAddress = edittextEmailAddress.getText().toString();
            String emailSubject = edittextEmailSubject.getText().toString();
            String emailText = edittextEmailText.getText().toString();
            String emailAddressList[] = {emailAddress};

            Intent intent = new Intent();
            intent.putExtra(Intent.EXTRA_EMAIL, emailAddressList);
            intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
            intent.putExtra(Intent.EXTRA_TEXT, emailText);

            if(arrayUri.isEmpty()){
                //Send email without photo attached
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("plain/text");
            }else if(arrayUri.size() == 1){
                //Send email with ONE photo attached
                intent.setAction(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_STREAM, arrayUri.get(0));
                intent.setType("image/*");
            }else{
                //Send email with MULTI photo attached
                intent.setAction(Intent.ACTION_SEND_MULTIPLE);
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayUri);
                intent.setType("image/*");
            }

            startActivity(Intent.createChooser(intent, "Choose App to send email:"));

        }};

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK){
            switch(requestCode){
                case RQS_LOADIMAGE:
                    Uri imageUri = data.getData();
                    arrayUri.add(imageUri);
                    myFileListAdapter.notifyDataSetChanged();
                    break;
                case RQS_SENDEMAIL:
                    break;
            }
        }
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    >


    <TextView
        android:layout_marginTop="20dp"
        android:id="@+id/email_txt_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Email To:"
        android:textStyle="bold"/>
    <TextView
        android:layout_marginTop="5dp"
        android:layout_below="@id/email_txt_view"
        android:id="@+id/email_address"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:text="abc@gmail.com"
        android:textStyle="bold|italic"
        android:textColor="@android:color/holo_blue_light"/>
    <TextView
        android:layout_marginTop="20dp"
        android:layout_below="@id/email_address"
        android:id="@+id/subject_txt_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Enter email Subject:"
        android:textStyle="bold"/>
    <EditText
        android:layout_below="@id/subject_txt_view"
        android:id="@+id/email_subject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailSubject"/>
    <TextView
        android:layout_marginTop="20dp"
        android:layout_below="@id/email_subject"
        android:id="@+id/emailBody_txt_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Enter Text:"
        android:textStyle="bold"/>
    <EditText
        android:layout_below="@id/emailBody_txt_view"
        android:id="@+id/email_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

   <ImageView
       android:layout_marginTop="20dp"
       android:layout_below="@id/email_text"
       android:id="@+id/attachment"
       android:layout_width="60dp"
       android:layout_height="60dp"
       android:src="@drawable/attachment"/>

    <ImageView
        android:layout_marginTop="20dp"
        android:layout_below="@id/email_text"
        android:layout_toRightOf="@id/attachment"
        android:id="@+id/send"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/send" />

    <ListView
        android:layout_below="@id/send"
        android:id="@+id/filelist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>
Guneet Kaur
  • 544
  • 1
  • 8
  • 26
  • Is there any error? Must sure you'r getting proper uri from your list. – Piyush Mar 02 '17 at 06:25
  • @Piyush i am able to select and send photos from camera but not from anything else! Please mention the code required to be changed! – Guneet Kaur Mar 02 '17 at 06:28
  • Debug your code which condition it matches ! – Piyush Mar 02 '17 at 06:32
  • please provide code anybody!!!! – Guneet Kaur Mar 02 '17 at 06:36
  • For single image or multiple ? – Piyush Mar 02 '17 at 06:37
  • @piyush : multiple – Guneet Kaur Mar 02 '17 at 06:55
  • Check [1](http://stackoverflow.com/questions/18225028/multiple-photo-not-sending-to-gmail-using-intent) ,[2](http://code2care.org/pages/share-multiple-images-in-whatsapp-using-android-intent/) ,[3](http://grishma102.blogspot.in/2015/04/sharing-of-single-multiple-images-text.html) and [4](http://stackoverflow.com/questions/13378108/androidselecting-multiple-images-in-gallery-and-starting-implicit-intent) – Piyush Mar 02 '17 at 07:00

1 Answers1

0

Sending Email in Android using JavaMail API without using the default/built-in app

try this way...advantage of this method is you don't have to send implicit intent to the mail activity.. you can send mail from your app itself..disadvantage is you have to turn on less secured app option in your email..check that link till end

Community
  • 1
  • 1
Kunal Parikh
  • 463
  • 4
  • 15