2013-08-01 40 views
24

Tôi có danh sách tệp trong ứng dụng Android của mình và tôi muốn có thể nhận các mục đã chọn và gửi chúng qua email hoặc bất kỳ ứng dụng chia sẻ nào khác. Đây là mã của tôi.Chia sẻ tệp trên Android, bằng cách gửi chúng qua email hoặc các ứng dụng khác

Intent sendIntent = new Intent(); 
        sendIntent.setAction(Intent.ACTION_SEND); 
        sendIntent.putExtra(Intent.EXTRA_EMAIL, getListView().getCheckedItemIds()); 
        sendIntent.setType("text/plain"); 
        startActivity(sendIntent); 

Trả lời

20
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(exportPath)); 

bạn cũng có thể làm cho zip file của tất cả các tập tin và đính kèm file zip cho gửi nhiều tập tin trong android

+1

Cảm ơn nhiều! Vấn đề của tôi được giải quyết. – DmitryKanunnikoff

1

Sử dụng ACTION_SEND_MULTIPLE cho việc cung cấp nhiều dữ liệu cho người

intent.setAction(Intent.ACTION_SEND_MULTIPLE); 
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayUri); 
intent.setType("text/plain"); 
startActivity(intent); 

Các arrayUri là Danh sách mảng của Uri của các tập tin để gửi.

-3

đã đọc bài viết này về Sending Content to Other Apps

Intent sendIntent = new Intent(); 

sendIntent.setAction(Intent.ACTION_SEND); 

sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); 

sendIntent.setType("text/plain"); 

startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); 
28

đây là đoạn code để chia sẻ tập tin trong android

Intent intentShareFile = new Intent(Intent.ACTION_SEND); 
File fileWithinMyDir = new File(myFilePath); 

if(fileWithinMyDir.exists()) { 
    intentShareFile.setType("application/pdf"); 
    intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+myFilePath)); 

    intentShareFile.putExtra(Intent.EXTRA_SUBJECT, 
         "Sharing File..."); 
    intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File..."); 

    startActivity(Intent.createChooser(intentShareFile, "Share File")); 
} 
+0

Trước hết, không có những thứ như * mã * cho một vấn đề nhất định. Đây là * một * giải pháp, nhưng có nhiều hơn nữa. Thứ hai, hãy giải thích mã của bạn làm gì và cách giải quyết vấn đề. – DennisW

+0

myFilePath là đường dẫn tệp PDF trong ví dụ trên. bạn có thể tìm thấy nhiều giải pháp để chọn tệp từ thẻ SD trong Android. chương trình trên chỉ lấy đường dẫn tệp làm đầu vào và hiển thị ứng dụng trên thiết bị để chia sẻ tệp. ví dụ. nếu bạn chọn ứng dụng gmail, nó sẽ đính kèm tập tin và thiết lập nội dung và văn bản chủ đề –

0

Dưới đây là một ví dụ để chia sẻ hoặc lưu một tập tin văn bản:

private void shareFile(String filePath) { 

    File f = new File(filePath); 

    Intent intentShareFile = new Intent(Intent.ACTION_SEND); 
    File fileWithinMyDir = new File(filePath); 

    if (fileWithinMyDir.exists()) { 
     intentShareFile.setType("text/*"); 
     intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filePath)); 
     intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "MyApp File Share: " + f.getName()); 
     intentShareFile.putExtra(Intent.EXTRA_TEXT, "MyApp File Share: " + f.getName()); 

     this.startActivity(Intent.createChooser(intentShareFile, f.getName())); 
    } 
} 
Các vấn đề liên quan