【问题标题】:Sharing mp3 data from Internal Storage从内部存储共享 mp3 数据
【发布时间】:2015-01-04 16:38:05
【问题描述】:

我正在尝试将我的声音私人保存在内部存储中,而不是共享它们,但是当我按下共享按钮时,我的应用程序总是崩溃

public void savesSound(){

    FileOutputStream fos;
    String FILENAME = "sharedsound.mp3";
    File f = new File(FILENAME);

    try {
        InputStream in = getResources().openRawResource(R.raw.ichbin);
        fos = new FileOutputStream(f);
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf, 0, buf.length)) != -1) {
            fos.write(buf, 0, len);
        }
        fos.close();
        Log.i("Tag","Finish");
    } catch (java.io.IOException e) {
        e.printStackTrace();
    }

}

和分享按钮

            FileInputStream fin = null;
            try {
                fin = openFileInput("sharedsound.mp3");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            Uri uri = Uri.parse(fin.toString());
            shareIntent.setType("audio/*");
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            shareIntent.putExtra(Intent.EXTRA_TEXT, "Hey Check this out Dude");
            startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

【问题讨论】:

    标签: android android-intent share


    【解决方案1】:

    首先,InputStream 上的toString() 不提供Uri.parse() 可以使用的路径。

    其次,您不能通过file://Uri与其他应用共享内部存储中的文件,因为其他应用无权访问该文件。

    您可以use FileProvider 将您的文件提供给第三方应用。这包含在the Android training moduleshere is a sample app 之一中,它们共享一个 PDF,最初打包为资产,来自内部存储。

    【讨论】:

    • 非常感谢您的帮助,但这并没有帮助我,因为我有一个 Soundboard 应用程序,我想在按下按钮时分享声音。
    • @Komeil:欢迎您使用FileProvider在用户按下按钮时分享声音。
    • 我终于可以设置这个功能了,我有了一个新的想法,我可以用一个按钮改变通知声音,你可能是个想法,我可以得到帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2011-10-27
    • 1970-01-01
    相关资源
    最近更新 更多