0

I'm trying to move from a Fragment to an Activity using Intents, and where I run my code, it causes android.os.TransactionTooLargeException.

My Fragment code (CardFragment.java):

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    txtName = view.findViewById(R.id.txt_name);
    btnFood = view.findViewById(R.id.btn_food);

    txtName.setText(mName);

    Bitmap bm = StringToBitMap(mImage);
    btnFood.setImageBitmap(bm);

    btnFood.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), Recipes.class);
            intent.putExtra("Name", mName);
            intent.putExtra("Recipe", mRecipe);
            intent.putExtra("Image", mImage);
            startActivity(intent);
        }
    });
}

The mName, mRecipe and mImage are String variables.

My Activity code (Recipes.java):

public class Recipes extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipes);

        TextView txtName = findViewById(R.id.txt_name);
        TextView txtRecipe = findViewById(R.id.txt_recipe);
        ImageView imgView = findViewById(R.id.img_view);

        txtName.setText(getIntent().getStringExtra("Name"));
        txtRecipe.setText(getIntent().getStringExtra("Recipe"));

        Bitmap bm = CardFragment.StringToBitMap(getIntent().getStringExtra("Image"));
        imgView.setImageBitmap(bm);
    }
}

Every time I run this code, I get this error message:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.company.recipeapp, PID: 12160
    java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 2355168 bytes
        at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:161)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.os.TransactionTooLargeException: data parcel size 2355168 bytes
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(BinderProxy.java:540)
        at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4408)
        at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145)
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

What is this error and how can I solve it? Maybe because there are multiple CardFragments in my previous view? Thanks for the help.

Matan
  • 169
  • 1
  • 10
  • Does this answer your question? [TransactionTooLargeException on API 28+ when sharing bitmap despite using Intent.EXTRA\_STREAM](https://stackoverflow.com/questions/56416395/transactiontoolargeexception-on-api-28-when-sharing-bitmap-despite-using-intent) – Ivo Oct 29 '21 at 12:40
  • Pass an identifier as the extra rather than your current values. Have the other activity use that identifier to get the needed data, such as retrieving it from a repository singleton. Or, perhaps you do not need a separate activity for that screen, and it can be another fragment within your original activity. – CommonsWare Oct 29 '21 at 12:41
  • Looks like `mImage` is probably not a `String`, but a relatively large bitmap. – David Wasser Nov 01 '21 at 22:31

1 Answers1

0

This error means you sending more than 1 MB of data through intent, in this case, your bitmap is too large for intent.

The solution is to reduce bitmap size or use different ways to pass bitmap like services, etc.

Dharman
  • 30,962
  • 25
  • 85
  • 135