将文件发送给其他用户

时间:2014-01-25 16:04:45

标签: android file send

我想通过我的应用程序将智能手机中的文件(gpx文件)发送给其他用户。 我不知道该怎么做。 如何使用电子邮件或短信发送它们(使用意图)?

我实现了一个文件浏览器,所以我可以在 FileChooser 类的 onFileLongClick 事件处理程序中处理它。

这是 FileChooser 类代码:

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();
    }
}

我需要你的建议!

更新 ListActivity没有“onLongListItemClick”方法。 :/

3 个答案:

答案 0 :(得分:0)

您可以通过启动电子邮件意图来发送文件。请查看以下主题以获取详细信息

Trying to attach a file from SD Card to email

答案 1 :(得分:0)

如果您要将文件发送给远离您的用户,您可以将文件附加到电子邮件中并轻松快速地发送! 请按照以下链接了解如何在Android中发送电子邮件并附加: How can I send emails from my Android application? http://www.javacodegeeks.com/2013/10/send-email-with-attachment-in-android.html

但如果您的用户在您附近,您可以使用蓝牙发送文件。

答案 2 :(得分:0)

好的,我做到了;)

我写了这个:

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"));

但是我怎么才能打开gmail?