我正在尝试使用意图从片段移动到活动,并且在我运行代码的地方,它会导致
android.os.TransactionTooLargeException
。
我的片段代码(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);
}
});
}
mName
、mRecipe
和mImage
是字符串变量。
我的活动代码(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);
}
}
每次运行此代码时,我都会收到此错误消息:
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)
这个错误是什么?我该如何解决它?也许是因为我之前的视图中有多个
CardFragment
?感谢您的帮助。
此错误意味着您通过 Intent 发送了超过 1 MB 的数据,在这种情况下,您的位图对于 Intent 来说太大了。
解决方案是减小位图大小或使用不同的方式传递位图之类的服务等
解决方案是本地缓存,github上有一个库可以做到这一点https://github.com/kernel0x/bundlesaver