2016-11-22 147 views
-2

在最近的時間,我用下面的代碼與其他應用程序共享MP3文件,如WhatsApp和一切正常,但現在我總是得到「Error2」吐司和文件獲勝不發送。 我讀了很多關於該主題的文章,但沒有什麼幫助。Android - 如何與其他應用程序共享聲音文件

MediaPlayer MP; 





public String ordnerpfad = Environment.getExternalStorageDirectory()+ "/Sounds";          
public String soundpfad = ordnerpfad + "/sound.mp3"; 
public File ordnerfile = new File(ordnerpfad); 
public File soundfile = new File(soundpfad); 
public Uri urisound = Uri.parse(soundpfad); 
public byte[] byte1 = new byte [1024]; 
public int zwischenspeicher = 0; 
public InputStream is1; 
public FileOutputStream fos; 
public Intent shareintent; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 



    //0 

    Button button = (Button) this.findViewById(R.id.button); 
    if (button != null) { 
     button.setOnClickListener(new View.OnClickListener() { 


      @Override 
      public void onClick(View v) { 


       stopPlaying(); 
       MP= MediaPlayer.create(MainActivity.this, R.raw.sound1); 
       MP.start(); 

      } 


     }); 
    } 
    button.setOnLongClickListener(new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 

      if(! ordnerfile.exists()) { 

       try { 
        ordnerfile.mkdir(); 
       } catch (Exception e){ 
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), "Error1", Toast.LENGTH_SHORT).show(); 
      } 




      } 

      try { 
       is1 = getResources().openRawResource(R.raw.sound1); 
       fos = new FileOutputStream(soundfile); 

       while ((zwischenspeicher = is1.read(byte1)) >0){ 


        fos.write(byte1, 0, zwischenspeicher); 
       } 

       is1.close(); 
       fos.close(); 


      }catch (Exception e){ 
       e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), "Error2", Toast.LENGTH_SHORT).show(); 

      } 

      shareintent = new Intent(Intent.ACTION_SEND); 
      shareintent .setType("audio/*"); 
      shareintent .putExtra(Intent.EXTRA_STREAM, urisound); 
      startActivity(Intent.createChooser(shareintent , "Share sound...")); 





      return true; 
     } 

    }); 



    //1 

    Button button1 = (Button) this.findViewById(R.id.button2); 
    if (button1 != null) { 
     button1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       stopPlaying(); 
       MP= MediaPlayer.create(MainActivity.this, R.raw.sound2); 
       MP.start(); 



      } 
     }); 
    } 
    button1.setOnLongClickListener(new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 

      if(! ordnerfile.exists()) { 

       try { 
        ordnerfile.mkdir(); 
       } catch (Exception e){ 
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), "Error1", Toast.LENGTH_SHORT).show(); 
       } 




      } 

      try { 
       is1 = getResources().openRawResource(R.raw.sound2); 
       fos = new FileOutputStream(soundfile); 

       while ((zwischenspeicher = is1.read(byte1)) >0){ 


        fos.write(byte1, 0, zwischenspeicher); 
       } 

       is1.close(); 
       fos.close(); 


      }catch (Exception e){ 
       e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), "Error2", Toast.LENGTH_SHORT).show(); 

      } 

      shareintent= new Intent(Intent.ACTION_SEND); 
      shareintent.setType("audio/*"); 
      shareintent.putExtra(Intent.EXTRA_STREAM, urisound); 
      startActivity(Intent.createChooser(shareintent, "Share sound...")); 

      return true; 
     } 
    }); 

清單:

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

'我總是得到「錯誤」吐司**其中**「錯誤吐司」? –

+0

在這裏創建的敬酒:} catch(Exception e){ e.printStackTrace(); Toast.makeText(getApplicationContext(),「Error」,Toast.LENGTH_SHORT).show(); } –

+0

把一個數字與你的敬酒像Error1,Error2 ...所以我們知道你得到哪個錯誤。 – saiful103a

回答

0

shareIntent.setType("audio/*");

後做

share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

相關問題