How to resolve java.lang.Runtimeexception error: android.os.Transactiontoolargeexception

Asked

Viewed 7,399 times

-1

Guys I’m having this error when I try to share a bitmap via Intent on Android 7.0, I’ve researched enough and could not solve, so I saw Google itself has the recommendation to avoid the error https://developer.android.com/guide/topics/resources/runtime-changes.html but I could not understand the proposed solution or find another alternative.

This is the Google sample code, I didn’t understand the Datafragment class what it is.

public class MyActivity extends Activity {

    private RetainedFragment dataFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // find the retained fragment on activity restarts
        FragmentManager fm = getFragmentManager();
        dataFragment = (DataFragment) fm.findFragmentByTag(“data”);

        // create the fragment and data the first time
        if (dataFragment == null) {
            // add the fragment
            dataFragment = new DataFragment();
            fm.beginTransaction().add(dataFragment, “data”).commit();
            // load the data from the web
            dataFragment.setData(loadMyData());
        }

        // the data is available in dataFragment.getData()
        ...
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // store the data in the fragment
        dataFragment.setData(collectMyLoadedData());
    }
}
  • 2

    what did you do? it would help if you showed the code...

  • I put the Google sample code I’m having the doubt.

1 answer

4

ERROR

This can occur when you pass large amount of data through parameters of a Intent!

When you receive this exception in your application, analyze if you are exchanging a lot of data between your services!

Using Intent to share huge data (for example, the user selects a huge number of gallery share files, the Uris of the selected files will be transferred using Intents).

In your case you’re trying to make a point (Bitmap) in the Intent save to somewhere (some system folder) and share only to URI.

Without your code, it gets harder to help!

CODE

The code you added above is a solution that instead of setting the parameters via Bundle

EXAMPLE:

    FragmentManager dataFragment = new FragmentManager();
    Bundle bundle = new Bundle();
    bundle.putSerializable("dataObject", loadMyData());   //parameters are (key, value).
    dataFragment.setArguments(bundle);
    fm.beginTransaction().add(dataFragment, “data”).commit();

You can add directly in via Set:

  // load the data from the web
   dataFragment.setData(loadMyData());

Fonte1

Fonte2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.