无法将音频文件附加到Samsung Devices

时间:2012-12-26 04:24:46

标签: android android-intent

我无法将音频文件附加到某些三星设备,如三星GalaxyS2,Nexus等。我不知道这是什么问题。我能够连接所有其他设备。请有人帮我解决这个问题。我可以代码如下:

Intent sendIntent = new Intent(Intent.ACTION_SEND);

            sendIntent.putExtra("sms_body",
                    getResources().getText(R.string.Message));
            sendIntent.setClassName("com.android.mms",
                    "com.android.mms.ui.ComposeMessageActivity");


            AssetManager mngr = getAssets();
            InputStream path = null;
            try {
                path = mngr.open("*.mp3");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            BufferedInputStream bis = new BufferedInputStream(path, 1024);

            // get the bytes one by one
            int current = 0;
            //
            try {
                while ((current = bis.read()) != -1) {

                    baf.append((byte) current);
                }
            } catch (IOException e) {
                // /TODO Auto-generated catch block
                e.printStackTrace();
            }
            byte[] bitmapdata = baf.toByteArray();

            File file = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath(), "*.mp3");

            // if (file.exists() == true) {
            // } else {
            // Log.v("directory is created", "new  dir");
            // file.mkdir();
            // }

            FileOutputStream fos;

            try {
                fos = new FileOutputStream(file);
                fos.write(bitmapdata);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                // handle exception
            } catch (IOException e) {
                // handle exception
            }

            final File file1 = new File(Environment
                    .getExternalStorageDirectory().getAbsolutePath(),
                    "*.mp3");
            Uri uri = Uri.fromFile(file1);

            Log.e("Path", "" + uri);

            sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
            sendIntent.setType("audio/mp3");
            // sendIntent.putExtra(Intent.EXTRA_STREAM, mms_uri.toString());

            startActivity(Intent.createChooser(sendIntent, ""));
            // startActivity(sendIntent);

        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    "SMS faild, please try again later!", Toast.LENGTH_LONG)
                    .show();
            e.printStackTrace();
        }

2 个答案:

答案 0 :(得分:1)

不要通过调用setClassName()显式设置类名。相反,只需按照here所述设置操作,输入和额外内容。

答案 1 :(得分:0)

您假设设备上有ExternalStorage即SD卡。您的S2或Nexus可能没有SD卡。我以前见过这个问题。