0

I would like to send the files in the smartphone (gpx files) to another user through my app. I don't know how to do this. How about send them using email or sms (Using intents) ?

I implemented a file explorer, so I can handle this in the onFileLongClick event handler in FileChooser class.

This is the FileChooser class codes :

public class FileChooser extends ListActivity {

private File currentDir;
private FileArrayAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    currentDir = Environment.getExternalStorageDirectory();
    fill(currentDir);
}
private void fill(File f)
{
    File[]dirs = f.listFiles();
    this.setTitle("Current Dir: "+f.getName());
    List<Item>dir = new ArrayList<Item>();
    List<Item>fls = new ArrayList<Item>();

    try {

        for(File ff: dirs) {

            Date lastModDate = new Date(ff.lastModified());
            DateFormat formater = DateFormat.getDateTimeInstance();
            String date_modify = formater.format(lastModDate);
            if(ff.isDirectory()) {

                File[] fbuf = ff.listFiles();
                int buf = 0;
                if(fbuf != null){
                    buf = fbuf.length;
                }
                else buf = 0;
                String num_item = String.valueOf(buf);
                if(buf == 0) num_item = num_item + " item";
                else num_item = num_item + " items";

                dir.add(new Item(ff.getName(),num_item,date_modify,ff.getAbsolutePath(),"directory_icon"));
            }
            else {
                fls.add(new Item(ff.getName(),ff.length() + " Byte", date_modify, ff.getAbsolutePath(),"file_icon"));
            }
        }
    }catch(Exception e) {
        e.printStackTrace();
    }
    Collections.sort(dir);
    Collections.sort(fls);
    dir.addAll(fls);
    if(!f.getName().equalsIgnoreCase("sdcard")) {
        dir.add(0,new Item("..","Parent Directory","",f.getParent(),"directory_up"));
    }
    adapter = new FileArrayAdapter(FileChooser.this,R.layout.row_custom_item, dir);
    this.setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Item o = adapter.getItem(position);

    try {
        if(o.getImage().equalsIgnoreCase("directory_icon")||o.getImage().equalsIgnoreCase("directory_up")){
            currentDir = new File(o.getPath());
            fill(currentDir);
        }
        else {
            onFileClick(o);
        }
    }catch(NullPointerException e) {
        Toast.makeText(this, "There's no a parent directory!" , Toast.LENGTH_SHORT).show();
    }
}

private void onFileLongClick(Item o) {

    HERE
}

private void onFileClick(Item o)
{
    String name = o.getName();
    int index = name.lastIndexOf(".");

    if(index != -1) {

        String estensione =  name.substring(index);

        if(estensione.compareToIgnoreCase(".GPX") == 0) {
            Intent intent = new Intent();
            intent.putExtra("GetPath",currentDir.toString());
            intent.putExtra("GetFileName",o.getName());
            setResult(RESULT_OK, intent);
            finish();
        }
        else {
            Toast.makeText(this, "Puoi importare solo file con estensione .GPX" , Toast.LENGTH_SHORT).show();
        }
    }
    else {
        Toast.makeText(this, "Puoi importare solo file con estensione .GPX" , Toast.LENGTH_SHORT).show();
    }
}

I need your suggestions !

Update: ListActivity has not "onLongListItemClick" method. :/

Loris
  • 454
  • 7
  • 19

3 Answers3

0

You can send the file by launching the email intent. Check the below thread for details

Trying to attach a file from SD Card to email

Community
  • 1
  • 1
Ajit Pratap Singh
  • 1,299
  • 12
  • 24
  • Try this code Check this answer http://stackoverflow.com/questions/11068648/launching-an-intent-for-file-and-mime-type File file = new File(filePath); MimeTypeMap map = MimeTypeMap.getSingleton(); String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName()); String type = map.getMimeTypeFromExtension(ext); if (type == null) type = "*/*"; Intent intent = new Intent(Intent.ACTION_VIEW); Uri data = Uri.fromFile(file); intent.setDataAndType(data, type); startActivity(intent); – Ajit Pratap Singh Jan 26 '14 at 16:39
0

If you want to send your files to users who are far away from you, you can attach the files to an email and send it easily and rapidly! Follow these links to learn how to send email in android and attach : How can I send emails from my Android application? http://www.javacodegeeks.com/2013/10/send-email-with-attachment-in-android.html

But if your users are near you, you can use Bluetooth to send the files.

Community
  • 1
  • 1
  • Have you a link for the bluetooth? – Loris Jan 26 '14 at 09:54
  • Here U R! : http://www.javacodegeeks.com/2013/09/bluetooth-data-transfer-with-android.html http://stackoverflow.com/questions/12562875/android-bluetooth-example http://www.javatpoint.com/android-bluetooth-tutorial I offer you to download this book => *Android Application Development Cookbook* It's a good reference for Android Programming – Seyed Hamed Shams Jan 26 '14 at 17:56
0

Ok i did it ;)

I wrote this:

File fileToSend = new File(currentDir.getPath() + "/" + o.getName());
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, o.getName());
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fileToSend));
sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the gpx file");
startActivity(Intent.createChooser(sendIntent, "Invia il file gpx"));

But how can i open gmail only?

Loris
  • 454
  • 7
  • 19