最近、私はWhatsappのような他のアプリケーションとmp3ファイルを共有するために次のコードを使用しましたが、すべてうまくいきましたが、今は常に "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;
}
});
マニフェスト:Androidの6と7では
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
「私はいつも「エラー」トースト」**「エラートースト」ですか? –
ここで作成されたトースト:} catch(Exception e){ e.printStackTrace(); Toast.makeText(getApplicationContext()、 "Error"、Toast.LENGTH_SHORT).show(); } –
トーストにError1、Error2 ...のような番号を付けて、どのエラーが表示されているのかわかります。 – saiful103a