-2

I examined similar subjects, but I couldn't do it. I'm trying to share .mp3 file with LongClick button. I found it for JPEG files. One guy created method for sharing jpeg file. How can I convert it for .mp3 files?

package com.example.tunch.trap;


import...

public class sansar extends AppCompatActivity {

private String yardik;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_sansar);

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

   yardik = createImageOnSDCard(R.raw.yardik_denizi);


    final MediaPlayer yardikdenizi = MediaPlayer.create(this, R.raw.yardik_denizi);
    Button btnYardik = (Button) findViewById(R.id.btnSansar_1);
    btnYardik.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(yardikdenizi.isPlaying()){

                yardikdenizi.seekTo(0);
            }
            yardikdenizi.start();
        }
    });



    btnYardik.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            Uri path= FileProvider.getUriForFile(sansar.this, "com.example.tunch.trap", new File(yardik));

            Intent shareYardik = new Intent();
            shareYardik.setAction(Intent.ACTION_SEND);
            shareYardik.putExtra(Intent.EXTRA_TEXT,"Bu ses dosyasını gönderiyorum");
            shareYardik.putExtra(Intent.EXTRA_STREAM, path);
            shareYardik.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            shareYardik.setType("audio/mp3");
            startActivity(Intent.createChooser(shareYardik, "Paylas.."));


return true;
        }
    });
}

private String createImageOnSDCard(int resID) {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resID);
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + resID +".mp3";
    File file = new File(path);
    try {
        OutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    }
    catch (Exception e){
        e.printStackTrace();
    }
    return file.getPath();
}
}

This is all Java code. createImageOnSDCard method is for images. I want to use it for my audio file (yardik_denizi.mp3). When I run this, it works but program is trying to send jpeg file. So it doesn't work literally :) How should I change that last part?

halfer
  • 19,824
  • 17
  • 99
  • 186
H. Tunc
  • 13
  • 7

3 Answers3

1

You need a method that copies a private raw resource content (R.raw.yardik_denizi) to a publicly readable file such that the latter can be shared with other applications:

 public void copyPrivateRawResuorceToPubliclyAccessibleFile(@RawRes int resID,
                                                                   @NonNull String outputFile) {
            InputStream inputStream = null;
            FileOutputStream outputStream = null;
            try {
                inputStream = getResources().openRawResource(resID);
                outputStream = openFileOutput(outputFile, Context.MODE_WORLD_READABLE 
                        | Context.MODE_APPEND);
                byte[] buffer = new byte[1024];
                int length = 0;
                try {
                    while ((length = inputStream.read(buffer)) > 0) {
                        outputStream.write(buffer, 0, length);
                    }
                } catch (IOException ioe) {
                    /* ignore */
                }
            } catch (FileNotFoundException fnfe) {
                /* ignore */
            } finally {
                try {
                    inputStream.close();
                } catch (IOException ioe) {
                    /* ignore */
                }
                try {
                    outputStream.close();
                } catch (IOException ioe) {
                    /* ignore */
                }
            }
        }

and then:

copyPrivateRawResuorceToPubliclyAccessibleFile(R.raw.yardik_denizi, "sound.mp3");
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("audio/*");
Uri uri = Uri.fromFile(getFileStreamPath("sound.mp3"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Sound File"));
Akaki Kapanadze
  • 2,552
  • 2
  • 11
  • 21
  • Tried but error ------Process: com.example.tunch.trap, PID: 17847 java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileOutputStream.close()' on a null object reference – H. Tunc Aug 06 '18 at 21:38
0

You should change the path for uri at

Uri path= FileProvider.getUriForFile(sansar.this, "com.example.tunch.trap", new File(change_it_path_to_yardik_denizi.mp3));
H. Ekici
  • 199
  • 5
0

Finally i got the answer. I can send mp3 files to other apps with this code.

 copyFiletoExternalStorage(R.raw.yardik_denizi, "yardik_denizi.mp3");
            Uri path= FileProvider.getUriForFile(sansar.this, 
"com.example.tunch.trap", new File(Environment.getExternalStorageDirectory() + 
"/Android/data/yardik_denizi.mp3"));
            Intent shareYardik = new Intent();
            shareYardik.setAction(Intent.ACTION_SEND);
            shareYardik.putExtra(Intent.EXTRA_TEXT,"Bu ses dosyasını gönderiyorum");
            shareYardik.putExtra(Intent.EXTRA_STREAM, path);
            shareYardik.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            shareYardik.setType("audio/mp3");
            startActivity(Intent.createChooser(shareYardik, "Paylas.."));

And need to create a method to save data in external store.

private void copyFiletoExternalStorage (int resourceId, String resourceName){
    String pathSDCard = Environment.getExternalStorageDirectory() + "/Android/data/" 
+ resourceName;
    try{
        InputStream in = getResources().openRawResource(resourceId);
        FileOutputStream out = null;
        out = new FileOutputStream(pathSDCard);
        byte[] buff = new byte[1024];
        int read = 0;
        try {
            while ((read = in.read(buff)) > 0) {
                out.write(buff, 0, read);
            }
        } finally {
            in.close();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
H. Tunc
  • 13
  • 7