2014-01-25 31 views
0

我想通過我的應用程序將智能手機中的文件(gpx文件)發送給其他用戶。 我不知道該怎麼做。 如何使用電子郵件或短信發送(使用意圖)?發送文件給其他用戶

我實現了一個文件瀏覽器,這樣我就可以在文件選擇處理這個在onFileLongClick事件處理程序。

這是文件選擇類代碼:

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」方法。 :/

回答

0

您可以通過啓動電子郵件意向來發送文件。檢查以下線程細節

Trying to attach a file from SD Card to email

+0

好吧,我讀過它。但是,一個gpx文件的正確類型是什麼? – Loris

+0

試試這個代碼 檢查這個答案 http://stackoverflow.com/questions/11068648/launching-an-intent-for-file-and-mime-type 檔案文件=新的文件(文件路徑); MimeTypeMap map = MimeTypeMap.getSingleton(); String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName()); String type = map.getMimeTypeFromExtension(ext);如果(type == null) type =「*/*」; Intent intent = new Intent(Intent.ACTION_VIEW); Uri data = Uri.fromFile(file); intent.setDataAndType(data,type); startActivity(intent); –

0

如果你想你的文件發送到誰遠從你的用戶,你可以將文件附加到電子郵件,輕鬆,快速地把它! 通過以下鏈接瞭解如何在Android中發送電子郵件,並附上: How can I send emails from my Android application? http://www.javacodegeeks.com/2013/10/send-email-with-attachment-in-android.html

但是,如果你的用戶在你的附近,你可以使用藍牙發送文件。

+0

你有藍牙鏈接嗎? – Loris

+0

這裏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 我爲您提供下載本書=> * Android應用程序開發指南* 這是一個很好的Android編程參考 –

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嗎?

相關問題