2013-08-01 46 views
5
Intent share = new Intent(Intent.ACTION_SEND); 
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + ContextID.getPackageName() + "/" + ResourceID)); 
share.setType("audio/*"); 
ContextID.startActivity(Intent.createChooser(share, "Condividi il suono")); 

Đoạn mã trên hoạt động tốt với Gmail, trong khi Whatsapp đưa ra một thông điệp chúc mừng như "Chia sẻ một tập tin được, vui lòng thử lại lần nữa"Chia sẻ qua WhatsApp

Có lẽ tôi đã cùng một vấn đề này guy: Intent.ACTION_SEND Whatsapp

Nhưng làm cách nào tôi có thể tạm thời sao chép tài nguyên của mình trên thẻ sd và sau đó chia sẻ chúng?

Trả lời

7
File dest = Environment.getExternalStorageDirectory(); 
InputStream in = ContextID.getResources().openRawResource(ResourceID);    

try 
{ 
    OutputStream out = new FileOutputStream(new File(dest, "lastshared.mp3")); 
    byte[] buf = new byte[1024]; 
    int len; 
    while ((len = in.read(buf, 0, buf.length)) != -1) 
    { 
     out.write(buf, 0, len); 
    } 
    in.close(); 
    out.close(); 
} 
catch (Exception e) {}    

Intent share = new Intent(Intent.ACTION_SEND); 
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().toString() + "/lastshared.mp3")); 
share.setType("audio/*"); 
ContextID.startActivity(Intent.createChooser(share, "Condividi il suono \"" + TheButton.getText() + "\"")); 
return true; 

manifest:

<manifest ...> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
    ... 
</manifest> 
+1

Xin chào, tôi đang sử dụng mã của bạn để gửi tệp 'mp3' thông qua WhatsApp nhưng tôi gặp phải lỗi tương tự như 'Không gửi được, vui lòng thử lại'. Bạn có biết cách vượt qua điều này không? Cảm ơn trước –

+0

hoạt động của nó .. nếu bạn cố gắng chia sẻ trực tiếp từ nội dung, whatsapp sẽ không cho phép bạn. –

+0

Grazie Mille !! Làm tốt;) – Manza

0

hãy thay đổi dòng này từ mã của bạn và bạn sẽ có thể chia sẻ ghi ("audio/mp3") như dưới đây thay vì ("audio/*")

share.setType("audio/mp3"); 

này là bởi vì loại cổ phiếu cho whatsapp không hỗ trợ ("audio/*") hoặc ("*/*")

Các vấn đề liên quan