我正在尝试制作一个可以加入 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();
}
}