Android - 如何与其他应用共享声音文件
Android - How to share a sound file with other apps
最近,我使用以下代码与 Whatsapp 等其他应用程序共享 mp3 文件,一切正常,但现在我总是收到 "Error2" toast,但文件无法发送。
我阅读了很多关于该主题的文章,但没有任何帮助。
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"/>
在 Android 6 和 7 中,访问外部存储被视为必须在运行时请求的 'dangerous permission'。
https://developer.android.com/training/permissions/requesting.html
您还可以在系统设置中授予该权限(应用 > 您的应用 > 权限)
之后
shareIntent.setType("audio/*");
做
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
最近,我使用以下代码与 Whatsapp 等其他应用程序共享 mp3 文件,一切正常,但现在我总是收到 "Error2" toast,但文件无法发送。 我阅读了很多关于该主题的文章,但没有任何帮助。
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"/>
在 Android 6 和 7 中,访问外部存储被视为必须在运行时请求的 'dangerous permission'。
https://developer.android.com/training/permissions/requesting.html
您还可以在系统设置中授予该权限(应用 > 您的应用 > 权限)
之后
shareIntent.setType("audio/*");
做
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);