I'm trying to save a custom object when I click a menu item. The problem is its crashing. Before we start, when I try to read that data in another activity, that also crashes. Here is my code where I'm trying to save the data:
Here's a pastebin link.
my write function in the menu
myInfo.setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
Intent ourIntent = new Intent(Results.this, listTimes.class);
ourIntent.putExtra("Meeting", meetingObj);
try {
fos = new FileOutputStream(filename);
oos = new ObjectOutputStream(fos);
oos.writeObject(businesses.get(positionChecked));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//startActivity(ourIntent);
return true;
}
// My read function
private void getData()
{
try {
fis = openFileInput(filename);
ois = new ObjectInputStream(fis);
business = (Business) ois.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
ois.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Related
I am a newbie.
I'm developing an android (4.2) app to record some audio.
The code seems to run fine the first time, and it even saves the files on the SD Card.
But, if I try to record again, my app crashes.
This is my code:
StringVoiceFile = PathImagePage + "/"+book_id+"-"+pagenumber+".mp3";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(StringVoiceFile);
this is my function
public void start(){
String PathImagePage = AppDirectory+"/content/"+userid+"/books/"+book_id+"/";
StringVoiceFile = PathImagePage + "/"+book_id+"-"+pagenumber+".mp3";
File VoiceFile = new File(PathImagePage, +"-"+pagenumber+".mp3");
MediaPlayer m = new MediaPlayer();
if (VoiceFile.exists()){
try {
myAudioRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
m.setDataSource(StringVoiceFile);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
myAudioRecorder.start();
} catch (IllegalStateException e) {
Log.e(TAG, "start() failed = " + e );
}
stop.setEnabled(true);
stop.setVisibility(View.VISIBLE);
time.setEnabled(true);
time.setVisibility(View.VISIBLE);
time.setText(formatTime(m.getDuration()));
play.setEnabled(false);
play.setVisibility(View.INVISIBLE);
long now = System.currentTimeMillis();
startRecordingTimer(now);
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_start_again));
} else {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException e) {
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_nostart));
} catch (IOException e) {
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_nostart));
}
stop.setEnabled(true);
stop.setVisibility(View.VISIBLE);
time.setVisibility(View.VISIBLE);
time.setText(formatTime(m.getDuration()));
play.setEnabled(false);
play.setVisibility(View.INVISIBLE);
long now = System.currentTimeMillis();
startRecordingTimer(now);
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_start));
}
}
Please help me to solve this issue
I am getting a duration of my audio file , convert it to int , then convert int to string and then string to something like this 00:32 . Converted string is correct, but then is a problem with convert that string to data
Here is my code:
File file = new File(Environment.getExternalStorageDirectory()+"/MyImages/.audio1.wav");
MediaPlayer mp = new MediaPlayer();
FileInputStream fs = null;
FileDescriptor fd = null;
try {
fs = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fd = fs.getFD();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.setDataSource(fd);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int length = mp.getDuration()/1000;
mp.release();
String audiotime = String.valueOf(length);
SimpleDateFormat sdf = new SimpleDateFormat("mmss");
try {
Date d = sdf.parse(audiotime);
textView1.setText(audiotime);
} catch (ParseException ex) {
}
Two things, most important, you use the original String as text for your TextView. You should do something like textView1.setText(d.toString());
Secondly, you should use "mm:ss" as your format, instead of "mmss".
Note you could also use the format function of SimpleDateFormat. This will return a StringBuffer, which might be more appropriate since you only need the String representation
source: android documentation
I am trying to send a file using Android bluetooth sockets. I am getting the pairing requests but unfortunately the data is not being transferred
my Server side and client side is as follows:
Server: (for my application server is receiving the data)
public void run() {
super.run();
BluetoothAdapter mBtadapter = BluetoothAdapter.getDefaultAdapter();
Log.i("bluetooth Adapter", "got adapter "+ mBtadapter.getAddress());
try {
Ssocket = mBtadapter.listenUsingRfcommWithServiceRecord("CardXchange", uuid);
Log.i("Socket", "Socket Acquired "+ Ssocket.toString());
} catch (IOException e) {
e.printStackTrace();
}
while(true){
if(read_flag()==1){
try {
Toast.makeText(c, "flag is eventually 1", Toast.LENGTH_LONG).show();
Ssocket.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
try {
client_socket = Ssocket.accept();
if(client_socket!=null){
//this function is used to handle the connection when sockets get connected
manageConnection1(client_socket);
Ssocket.close();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
Log.i("breaked ", "oh god!! out of the loop ");
}
//implementation of the function
private void manageConnection1(BluetoothSocket client_socket2) {
// TODO Auto-generated method stub
int bytes;
Log.i("serevr chya manage connection madhe gela", "in servers manage connection ");
File file = new File("/sdcard/myfolder/received.Xcard");
try {
FileOutputStream fos = new FileOutputStream(file);
try {
InputStream is = client_socket2.getInputStream();
while((bytes = is.read(buffer, 0, 1024))>0){
fos.write(buffer, 0, 1024);
}
is.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
client_socket2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and the client side (the sending side is like this):
public void run(){
try {
Method m = send_dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
Csocket = (BluetoothSocket) m.invoke(send_dev, 1);
Csocket.connect();
manageSendConnection(Csocket);
}catch(Exception e){
e.printStackTrace();
}
}
//function to handle after connection is established.
private void manageSendConnection(BluetoothSocket csocket) {
// TODO Auto-generated method stub
File f = new File("/sdcard/myfolder/card3.Xcard");
byte[] buffer = new byte[(int)f.length()];
try {
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(buffer, 0, (int)f.length());
OutputStream os = csocket.getOutputStream();
os.write(buffer);
os.close();
fis.close();
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
csocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am getting the pairing request but the data is not being sent.
can anyone please help me to find the problem and give a hint on solution??
How to read the particular data from the internal storage file.
For eg., I have stored
1. Device 2. Time(epoch format) 3. button text
CharSequence cs =((Button) v).getText();
t = System.currentTimeMillis()/1000;
s = cs.toString();
buf = (t+"\n").getBytes();
buf1 = (s+"\n").getBytes();
try {
FileOutputStream fos = openFileOutput(Filename, Context.MODE_APPEND);
fos.write("DVD".getBytes());
fos.write(tab.getBytes());
fos.write(buf);
fos.write(tab.getBytes());
fos.write(buf1);
//fos.write(tab.getBytes());
//fos.write((R.id.bSix+"\n").getBytes());
fos.write(newline.getBytes());
//fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
then while reading, how can we read only price from the file? (using fos.read())
Thanks
I would suggest writing the file in a more structured way, like this:
long t = System.currentTimeMillis()/1000;
String s = ((Button) v).getText();
DataOutputStream dos = null;
try {
dos = new DataOutputStream(openFileOutput(Filename, Context.MODE_APPEND));
dos.writeUTF("DVD");
dos.writeLong(t); // Write time
dos.writeUTF(s); // Write button text
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
// Ignore
}
}
}
To read it back, something like this:
DataInputStream dis = null;
try {
dis = new DataInputStream(openFileInput(Filename));
String dvd = dis.readUTF();
long time = dis.readLong();
String buttonText = dis.readUTF();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
// Ignore
}
}
}
My app crashes when it tryes to load a file that isnt there. how do i stop it from loading the file when there isnt a file but when there is a file of that name it does load it.
Here is the code I am using to load the file. Any comments would be appreciated :)
Cheers
try {
fis = openFileInput(FILENAME1);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1){
task1 = new String(dataArray);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Try something like this:
fis = getContext().getFileStreamPath(FILENAME1);
if( fis.exists() ) {
...
}
else {
....
}