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;
public class MainActivity extends Activity {
EditText edittextEmailAddress;
EditText edittextEmailSubject;
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);
edittextEmailSubject = (EditText)findViewById(R.id.email_subject);
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);
}
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();
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, "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;
}
}
}
}
<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="Enter email Subject:"/>
<EditText
android:id="@+id/email_subject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailSubject"/>
<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>
Download the files.
Next:
- email send images exercise, with spinner of pre-defined subjects.
3 comments:
Awesome job. very useful
Great Post! Helped me understood multiple select much better. THankS!
How about when you click an item in the listview, a full image of that item will appear so that the user can check what the image of uri looked like before sending. Can you also discuss what's the code for that? Thank you and great tutorial btw :)
Post a Comment