Thursday, October 25, 2012

email send images exercise, with spinner of pre-defined subjects.

Refer to the last exercise "Start activity to send email with multiple images attached, with build-in MediaStore.Images.Media selector", it always send email with images attached. So I want to have a spinner with pre-defined email subjects for user to easy choice.

email send images exercise, with spinner of pre-defined subjects.

Create /res/values/array.xml to provide the pre-defined email subjects.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="subject">
    <item>My Photos</item>
    <item>Share with you</item>
    <item>Please check</item>
    <item>Hello Friend</item>
</string-array>
</resources>


Modify layout to replace EditText of email subject with a <Spinner>.
<LinearLayout 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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
    
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Enter email address:"/>
    <EditText 
        android:id="@+id/email_address"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Choice Subject:"/>
    <Spinner
        android:id="@+id/spinner_subject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Enter Text:"/>
    <EditText 
        android:id="@+id/email_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
    <Button
        android:id="@+id/send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send"/>
    <ListView
        android:id="@+id/filelist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>


Modify the code:
package com.example.androidselectmultifiles;

import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;

public class MainActivity extends Activity {
 
 EditText edittextEmailAddress;
 Spinner spinnerSubject;
    EditText edittextEmailText;
 Button btnSend;
 ListView listViewFiles;
 
 ArrayList<Uri> arrayUri = new ArrayList<Uri>();
 ArrayAdapter<Uri> myFileListAdapter;
 
    final int RQS_SENDEMAIL = 1;
    
    SimpleCursorAdapter listAdapter;
    
    final Uri sourceUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    ListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        edittextEmailAddress = (EditText)findViewById(R.id.email_address);
        edittextEmailText = (EditText)findViewById(R.id.email_text);
        
        btnSend = (Button)findViewById(R.id.send);
        btnSend.setOnClickListener(btnSendOnClickListener);

        listViewFiles = (ListView)findViewById(R.id.filelist);
        String[] from = {MediaStore.MediaColumns.TITLE};
        int[] to = {android.R.id.text1};

        CursorLoader cursorLoader = new CursorLoader(
          this, 
          sourceUri, 
          null, 
          null, 
          null, 
          MediaStore.Audio.Media.TITLE);
        
        Cursor cursor = cursorLoader.loadInBackground();
        listAdapter = new SimpleCursorAdapter(
          this, 
          android.R.layout.simple_list_item_multiple_choice, 
          cursor, 
          from, 
          to, 
          CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        
        listViewFiles.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
        listViewFiles.setAdapter(listAdapter);
        
        //Prepare Spinner for subject
        spinnerSubject = (Spinner)findViewById(R.id.spinner_subject);
        ArrayAdapter<CharSequence> adapterSpinnerSubject = ArrayAdapter.createFromResource(this, R.array.subject, android.R.layout.simple_spinner_item);
        adapterSpinnerSubject.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerSubject.setAdapter(adapterSpinnerSubject);
    }
    
    private ArrayList<Uri> getSelectedFiles(){
     ArrayList<Uri> arrayU = new ArrayList<Uri>();
     
     SparseBooleanArray checkedArray = listViewFiles.getCheckedItemPositions();
     
     for(int i=0; i<checkedArray.size(); i++)
        {
      int pos = checkedArray.keyAt(i);
      Cursor cursor = listAdapter.getCursor();
      cursor.moveToPosition(pos);
      String _id = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID));
      Uri selUri = Uri.withAppendedPath(sourceUri, _id);
      arrayU.add(selUri);
        }
     
     return arrayU;
    }
  
 OnClickListener btnSendOnClickListener
 = new OnClickListener(){

  @Override
  public void onClick(View v) {
   
   arrayUri = getSelectedFiles();

   String emailAddress = edittextEmailAddress.getText().toString();
   
   //retrieve spinnerSubject selection
   String emailSubject = spinnerSubject.getSelectedItem().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, "Choice App to send email:"));
  }};

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

  super.onActivityResult(requestCode, resultCode, data);
  
  if (resultCode == RESULT_OK){
   switch(requestCode){
   case RQS_SENDEMAIL:
    break; 
   }
  }
 }

}


download filesDownload the files.


1 comment:

  1. Awesome great work! any way to do the email addresses also?

    ReplyDelete