Select image and send it by mail // Share attachment in mail

0

Good morning. I'm trying to make an application that allows you to select an image (from the gallery or any place where there are images) and send it by mail. I was looking everywhere but I can not manage to send the image, sending only text is simple ... but with the image becomes complicated. I attach a code that I think is the most likely to walk. It is not my property, then I leave the link of the creator. link

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {

EditText editTextEmail, editTextSubject, editTextMessage;
Button btnSend, btnAttachment;
String email, subject, message, attachmentFile;
Uri URI = null;
private static final int PICK_FROM_GALLERY = 101;
int columnIndex;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editTextEmail = (EditText) findViewById(R.id.editTextTo);
    editTextSubject = (EditText) findViewById(R.id.editTextSubject);
    editTextMessage = (EditText) findViewById(R.id.editTextMessage);
    btnAttachment = (Button) findViewById(R.id.buttonAttachment);
    btnSend = (Button) findViewById(R.id.buttonSend);

    btnSend.setOnClickListener(this);
    btnAttachment.setOnClickListener(this);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
        /**
         * Get Path
         */
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
        cursor.moveToFirst();
        columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        attachmentFile = cursor.getString(columnIndex);
        Log.e("Attachment Path:", attachmentFile);
        URI = Uri.parse("file://" + attachmentFile);
        cursor.close();
    }
}

@Override
public void onClick(View v) {

    if (v == btnAttachment) {
        openGallery();

    }
    if (v == btnSend) {
        try {
            email = editTextEmail.getText().toString();
            subject = editTextSubject.getText().toString();
            message = editTextMessage.getText().toString();

            final Intent emailIntent = new Intent(
                    android.content.Intent.ACTION_SEND);
            emailIntent.setType("plain/text");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                    new String[] { email });
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    subject);
            if (URI != null) {
                emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
            }
            emailIntent
                    .putExtra(android.content.Intent.EXTRA_TEXT, message);
            this.startActivity(Intent.createChooser(emailIntent,
                    "Sending email..."));

        } catch (Throwable t) {
            Toast.makeText(this,
                    "Request failed try again: " + t.toString(),
                    Toast.LENGTH_LONG).show();
        }
    }

}

public void openGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.putExtra("return-data", true);
    startActivityForResult(
            Intent.createChooser(intent, "Complete action using"),
            PICK_FROM_GALLERY);

}
}

activity_main.xml

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:padding="5dp" >

    <EditText
        android:id="@+id/editTextTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="5dp"
        android:hint="Email Address!"
        android:inputType="textEmailAddress"
        android:singleLine="true" />

    <EditText
        android:id="@+id/editTextSubject"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/editTextTo"
        android:layout_margin="5dp"
        android:hint="Subject"
        android:singleLine="true" />

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@id/editTextSubject"
        android:layout_margin="5dp"
        android:gravity="top|left"
        android:hint="type message here!"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:layout_below="@id/editTextMessage"
        android:layout_margin="5dp"
        android:text="Send" />

    <Button
        android:id="@+id/buttonAttachment"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="attachment" />
</RelativeLayout>

Manifest permissions

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

I think the error arises when you try to get the address of the file. I hope you can help me with this problem, or guide me in the creation of another code. What I need is for the user to choose the image that he wants to send and this was sent by email.

    
asked by Mauro 25.10.2018 в 16:39
source

0 answers