Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize android.net.Uri object

I want to serialize the state of an object of type android.net.Uri.

Below is my model class with writeObject and readObject methoods

public class ReminderObject implements Serializable {
private boolean isReminderOn;
private int fromHours, toHours;
private int interval;
private ArrayList<CharSequence> daysToRepeat;
private Uri toneToPlay;
private AdvanceSettingsObject adv;

public ReminderObject(boolean isReminderOn, int fromHours, int toHours,
        int interval, ArrayList<CharSequence> daysToRepeat, Uri toneToPlay,
        AdvanceSettingsObject adv) {
    super();
    this.isReminderOn = isReminderOn;
    this.fromHours = fromHours;
    this.toHours = toHours;
    this.interval = interval;
    this.daysToRepeat = daysToRepeat;
    this.toneToPlay = toneToPlay;
    this.adv = adv;
} 
/*
getters and setters
*/
    public void writeObject(ObjectOutputStream op){
    try {
        op.defaultWriteObject();
        op.writeChars(toneToPlay.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public void readObject(ObjectInputStream in){
    try{
        in.defaultReadObject();
        toneToPlay = Uri.parse(in.readUTF());
    }catch(Exception e){

    }
}

}

Code snippet for MainActivity:

try {
        ObjectOutputStream os = new ObjectOutputStream(new     FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/ReminderData.txt")    );
        os.writeObject(reminder); // Getting above mentioned Exception here
        Log.i("TAG","reminder serialized");
        ObjectInputStream is = new ObjectInputStream(new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/ReminderData.txt"));
        ReminderObject reminderRead = (ReminderObject) is.readObject();
        if(reminderRead!=null)
            Log.i("TAG", "Deserialized Reminder object is : "+reminderRead.toString());
        else{
            Log.i("TAG", "Null received");
        }
    } catch(ClassNotFoundException cnf){
        cnf.printStackTrace();
    } catch (FileNotFoundException e) {     
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Exception faced :

03-31 23:47:59.246: W/System.err(12681): java.io.NotSerializableException: android.net.Uri$HierarchicalUri 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481) 03-31 23:47:59.246: W/System.err(12681): at com.navkar.navkarreminder.SetReminderActivity.scheduleReminder(SetReminderActivity.java:595)

Requesting help.

like image 892
shahharshil46 Avatar asked Mar 31 '14 18:03

shahharshil46


3 Answers

URI private Uri toneToPlay is not serializable. So there is an alternative way, you can change the data type URI to String and convert your URI to string and string to URI while use in vise versa.

Uri to String

Uri uri;
String stringUri;
stringUri = uri.toString();

String to Uri

Uri uri;
String stringUri;
uri = Uri.parse(stringUri);
like image 143
King of Masses Avatar answered Sep 20 '22 10:09

King of Masses


As i think problem is the data member that you saving problem .

private Uri toneToPlay is a non serialize data type and you cannot serialize it.

like image 41
User10001 Avatar answered Sep 23 '22 10:09

User10001


Consider using java.net.URI class instead. This has almost the same functionality, but it's Serializable. You can easily convert from Uri to URI (and backwards):

Uri to URI: new URI(uri.toString())

URI to Uri: Uri.parse(uri.toString())

like image 33
Mike Keskinov Avatar answered Sep 21 '22 10:09

Mike Keskinov