Search code examples
javaandroidbundle

Get data from getter method instead of passing in a Bundle


I need to pass some Bitmaps from one activity to another, and, since the size limit of the Bundle won't let me pass these images (even using a byte array*), I thought that I could use a getter method between these Activities.

-But, since I'm still not a master in Android (Java), I don't know if that would make any difference, and, if it does, what should I watch out for when using it.

the byte array did reduce the total size(at about 60%), but it still wasn't enough

scaling down is a way out, but just in case any other solution works


Solution

  • save your object in a file

        private void saveDataToFile() {
    
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = getContext().openFileOutput("fileName", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
    
    }
    ObjectOutputStream objectOutputStream = null;
    try {
        objectOutputStream = new ObjectOutputStream(fileOutputStream);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
    
    }
    try {
        if (objectOutputStream != null) {
            objectOutputStream.writeObject(yourObject); //which data u want to save
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        if (objectOutputStream != null) {
            objectOutputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    }

    Retrieve the object from another activity

    private void getDataFromFile() {
    
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = getContext().openFileInput("fileName");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return;
    }
    ObjectInputStream objectInputStream = null;
    try {
        objectInputStream = new ObjectInputStream(fileInputStream);
    } catch (IOException |NullPointerException e) {
        e.printStackTrace();
    }
    try {
        yourObject = (ObjectClass) objectInputStream.readObject();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        objectInputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    }