2016-11-22 1 views
-2

최근에는 Whatsapp와 같은 다른 앱과 mp3 파일을 공유하기 위해 다음 코드를 사용했지만 모든 것이 정상적으로 작동했지만 지금은 항상 "Error2"토스트와 파일을 얻습니다. 보내지 마. 그 주제에 관한 많은 기사를 읽었지만 아무 것도 정말로 도움이되지 않았습니다.안드로이드 - 다른 앱과 사운드 파일을 공유하는 방법

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; 
     } 
    }); 

매니페스트 안드로이드 6 및도 7에

<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

에 해당 권한을 부여 할 수 있습니다

share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

관련 문제