2015-11-02 182 views
0

我正在製作一個音板應用程序,並且在按鈕1被長時間按下時我需要共享sound1。我可以讓共享菜單COM了這個代碼:將文件保存到外部存儲器中的文件夾並共享

Button button1; 


    button1 = (Button) v.findViewById(R.id.button1); 
    button1.setLongClickable(true); 

    button1.setOnLongClickListener(new View.OnLongClickListener() { 

     @Override 
     public boolean onLongClick(View arg0) { 

      Intent shareIntent = new Intent(Intent.ACTION_SEND); 
      shareIntent.setType("audio/ogg"); 

      Uri.parse("android.resource://test.testapp/" + R.raw.sound1); 
      shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://test.testapp/" + R.raw.sound1)); 
      startActivity(Intent.createChooser(shareIntent, "Share Sound")); 

      return true; 
     } 

    }); 


    return v; 
} 

我可以用WhatsApp的和谷歌驅動器完全共享的音頻文件,但其他應用程序不工作。我讀過,你必須將文件複製到外部存儲,並從那裏分享。我已經搜索了近兩天,但我找不到一種方法來做到這一點。有關Stack的其他文章也不幫助我:/

如何在外部存儲中創建一個目錄,將文件(sound1.ogg)從my/raw文件夾複製到該文件夾​​,然後將該聲音與另一個應用程序(Gmail,Google Drive,Whatsapp,Skype等)?

+0

你有沒有看到這篇文章? http://stackoverflow.com/questions/17794974/create-folder-in-android – Mario

回答

0

將某物保存到外部存儲器非常簡單。 首先,你需要把它添加到您的清單文件,以允許外部存儲寫作:

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

然後在你的活動/片段:

String root = Environment.getExternalStorageDirectory().toString(); 
File myDir = new File(root + "/saved_images"); 
myDir.mkdirs(); 
File file = new File (myDir, "FILE_NAME"); 
... however you write the file through output stream .... 

由於這些是音頻文件,你應該把它們存儲在在每個用戶的android手機上公共音頻庫。你可以像這樣訪問:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) 

我會建議通過this article更多信息看!

+0

只需添加到答案:將這些音頻文件保存到公共音頻文件夾允許任何應用程序能夠訪問它!因此,如果應用允許發佈或上傳任何類型的音頻文件,只要將它們保存在此文件夾中,就可以訪問它們。 – Shrey

+0

我不認爲它們應該存儲在音樂文件夾中,因爲它們確實很短(1-6秒) – Spickle

相關問題