我想将此代码转换为声音(尝试共享音频文件)

时间:2018-08-06 20:15:35

标签: java android android-studio

我检查了类似的科目,但我做不到。我正在尝试使用LongClick按钮共享.mp3文件。我发现它是用于JPEG文件的。一个人创建了共享jpeg文件的方法。如何将其转换为.mp3文件?

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();
}
}

这是所有Java代码。 createImageOnSDCard方法用于图像。我想将其用于我的音频文件(yardik_denizi.mp3)。当我运行它时,它可以工作,但是程序试图发送jpeg文件。所以从字面上看是行不通的:)我应该如何更改最后一部分?

3 个答案:

答案 0 :(得分:1)

您需要一种将私有原始资源内容(R.raw.yardik_denizi)复制到公共可读文件的方法,以便后者可以与其他应用程序共享:

 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 */
                }
            }
        }

然后:

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"));

答案 1 :(得分:0)

您应在

处更改uri的路径。
Uri path= FileProvider.getUriForFile(sansar.this, "com.example.tunch.trap", new File(change_it_path_to_yardik_denizi.mp3));

答案 2 :(得分:0)

最后我得到了答案。我可以使用此代码将mp3文件发送到其他应用。

 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.."));

并且需要创建一种将数据保存在外部存储中的方法。

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();
    }

}