Reputation: 79
I am trying to make a program which can join 2 MP3 files and save them on the android SD card. I have Java code that is working but when I try to convert it to Android it gives some error.
In Java code is written below. It's working perfect.
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();
}
}
In Android, what I'm trying to do is:
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();
}
}
Upvotes: 3
Views: 3069
Reputation: 33534
- Give this permission WRITE_EXTERNAL_STORAGE
Here is the working code from my project:
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();
}
}
});
}
}
Upvotes: 1
Reputation: 59168
You need to give your app the permission to write to the SD Card by adding the line below to your Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Upvotes: 0