3

我正在尝试制作一个可以加入 2 个 MP3 文件并将它们保存在 android SD 卡上的程序。我有可以运行的 Java 代码,但是当我尝试将其转换为 Android 时,它会出现一些错误。

下面用 Java 编写代码。它工作完美。

import java.io.*;
public class TuneDoorJava {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream fistream1 = new FileInputStream("F:\\aa.mp3");  // first source file
        FileInputStream fistream2 = new FileInputStream("F:\\bb.mp3");//second source file
        SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
        FileOutputStream fostream = new FileOutputStream("F:\\final.mp3");//destinationfile

        int temp;

        while( ( temp = sistream.read() ) != -1)
        {
            // System.out.print( (char) temp ); // to print at DOS prompt
            fostream.write(temp);   // to write to file
        }
        fostream.close();
        sistream.close();
        fistream1.close();
        fistream2.close();
    }
    }

在Android中,我想做的是:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      //  FileOutputStream fostream=null;
        FileInputStream fist=(FileInputStream)getResources().openRawResource(R.raw.t);
        FileInputStream fist2=(FileInputStream)getResources().openRawResource(R.raw.v);

        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath() + "/dir1");
        dir.mkdirs();
        File file = new File(dir, "filename");

       //FileInputStream fistream1 = new FileInputStream();  // first source file
        //FileInputStream fistream2 = new FileInputStream("F:\\bb.mp3");//second source file
        SequenceInputStream sistream = new SequenceInputStream(fist, fist2);

        FileOutputStream fostream = new FileOutputStream(file);

        int temp;
        while( ( temp = sistream.read() ) != -1)
        {
            // System.out.print( (char) temp ); // to print at DOS prompt
            fostream.write(temp);   // to write to file
        }
        fostream.close();
        sistream.close();
        fistream1.close();
        fistream2.close();
    }
}
4

2 回答 2

1

-授予此权限WRITE_EXTERNAL_STORAGE

这是我项目中的工作代码:

public class ConcateSongActivity extends Activity {
    Button mbutt;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mbutt = (Button)findViewById(R.id.button_Click_Karo);
        mbutt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                try {
                    FileInputStream fis1 = new FileInputStream("/sdcard/viv0.wav");
                    FileInputStream fis2 = new FileInputStream("/sdcard/viv1.wav");
                    SequenceInputStream sis = new SequenceInputStream(fis1,fis2);


                    FileOutputStream fos = new FileOutputStream(new File("/sdcard/vis.wav"));

                    int temp;

                    try {
                        while ((temp = sis.read())!= -1){

                            fos.write(temp);

                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }




                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }
        });
    }
}
于 2012-12-01T05:18:17.900 回答
0

您需要通过在您的清单中添加以下行来授予您的应用写入 SD 卡的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-12-01T05:12:43.477 回答