0

I have various audio files in the raw folder in my Android project. My code is not working. When I click the share button application, it crashes.

This is my code:

public void onClick(View v) {
    InputStream inputStream;
    FileOutputStream fileOutputStream;
    try {
        inputStream = getResources().openRawResource(R.raw.numer_1);
        fileOutputStream = new FileOutputStream(
            new File(Environment.getExternalStorageDirectory(), "sound.mp3"));

        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            fileOutputStream.write(buffer, 0, length);
        }
        inputStream.close();
        fileOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM,
    Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/sound.mp3" ));
    intent.setType("audio/*");
    startActivity(Intent.createChooser(intent, "Share sound"));
}

And Android manifest add:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
KevinO
  • 4,303
  • 4
  • 27
  • 36
W4R1AT
  • 21
  • 5
  • What is the crash you are getting? Can you share with us your logcat? – Chris Stillwell Nov 14 '18 at 17:31
  • @ChrisStillwell Unfortunately I do not have logcat. I have problems with the android studio. I'm testing applications on my phone. – W4R1AT Nov 14 '18 at 17:36
  • have a look at this solution and see if it works for you: https://stackoverflow.com/a/18687297/7098620 – K.D. MBEDOBE Nov 14 '18 at 17:36
  • @W4R1AT, if you have Android Studio you have logcat... What is the exception your are getting in your log when the app crashes? – Chris Stillwell Nov 14 '18 at 17:39
  • @K.D.MBEDOBE I looked at all topics about share audio raw. I could not find anything that would help me. – W4R1AT Nov 14 '18 at 17:43
  • @ChrisStillwell I create an apk and start on the phone.Then, when you click share, the application closes. – W4R1AT Nov 14 '18 at 17:45
  • What API is your phone? In newer APIs you have to explicitly ask for permissions, it isn't enough to just request them in the manifest. You should also load your app through the Run or Debug options in Android Studio rather than just building an APK. Both will give you important logging information and better debugging capabilities. – Chris Stillwell Nov 14 '18 at 17:52
  • @ChrisStillwell My phone has api 28. I wanted to write applications from 22 to 28. How to make it work in this range? Forgive me my English. – W4R1AT Nov 14 '18 at 17:56
  • You need to ask permission then. Call [ActivityCompat.requestPermissions()](https://developer.android.com/reference/android/support/v4/app/ActivityCompat#requestPermissions(android.app.Activity,%20java.lang.String[],%20int)), see if that clears your error. If it doesn't, I highly recommend you connect to your debugger and post the error you get here. We won't be able to help you much otherwise. – Chris Stillwell Nov 14 '18 at 18:02
  • @ChrisStillwell Even with the privileges granted, I do not want to act. – W4R1AT Nov 14 '18 at 19:15

1 Answers1

1
if you use this your app will not crash again. The problem is at **Environment.getExternalStorageDirectory()**you're missing **.getPath()**

use the code below as guide for your intent.

final Intent audIntent = new Intent(android.content.Intent.ACTION_SEND);
                                     String audioName="kidi.mp3";
                                     audIntent.setType("audio/mp3");
                                     audIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file://"+"/Environment.getExternalStorageDirectory().getPath()/"+audioName));
                                     startActivity(Intent.createChooser(audIntent, "Share Audio "));
K.D. MBEDOBE
  • 43
  • 1
  • 12