我想将mp3文件复制到外部存储器,我可以通过whatsapp或类似的东西共享它。
我遇到了一些问题:
java.io.FileNotFoundException:/ storage / emulated / 0 / Music / Arno lacht。 (是目录)
我如何创建文件?我是编程sry的新手......
@覆盖 public void onRequestPermissionsResult(int requestCode,@ NonNull String [] permissions,@ NonNull int [] grantResults){ super.onRequestPermissionsResult(requestCode,permissions,grantResults);
switch (requestCode){
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNALSTORAGE:{
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
// External Storage accessible?
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
try{
// Create path
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) ;
File root = new File(path, "Sounds");
if(!root.exists()) {
// Create new directory
root.mkdirs();
Toast.makeText(getApplicationContext(), "Ordner wurde erstellt", Toast.LENGTH_SHORT).show();
}
// Make sure the music directory exists.
File file = new File(root, soundNamenArray.get(itemPosition) );
// Copy data from internal memory to external memory
InputStream is = getResources().openRawResource(soundId[itemPosition]);
FileOutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
int length;
while (( length = is.read(data)) > 0){
os.write(data, 0, length);
}
is.close();
os.close();
Toast.makeText(getApplicationContext(), "Datei kopiert!", Toast.LENGTH_SHORT).show();
uri = Uri.fromFile(file);
}catch (IOException e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Exception geworfen", Toast.LENGTH_SHORT).show();
}
// Intent zum verschicken Starten
teilenIntent = new Intent(Intent.ACTION_SEND);
teilenIntent.setType("audio/mp3");
teilenIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(teilenIntent, "Sound teilen"));
}else {
Toast.makeText(getApplicationContext(), "Fehler", Toast.LENGTH_SHORT).show();
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}