我正在尝试将位图传递给另一个 Activity,并且我正在使用 ImageView
显示来自另一个 Activity 的相同图像。这就是我传递位图的方式。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == CAMERA_RESULT) {
File out = new File(getFilesDir(), "newImage.jpg");
if(!out.exists()) {
Toast.makeText(getBaseContext(),
"Error while capturing image", Toast.LENGTH_LONG)
.show();
return;
}
Bitmap mBitmap = BitmapFactory.decodeFile(out.getAbsolutePath());
Intent bitIntent = new Intent(this, CameraTake.class);
bitIntent.putExtra("BitmapImage", mBitmap);
startActivity(bitIntent);
这就是我获得值(value)的方式:
Intent intent = getIntent();
bitmap= (Bitmap)intent.getParcelableExtra("BitmapImage");
ImageView im1 = (ImageView)findViewById(R.id.camOut);
im1.setImageBitmap(bitmap);
在运行应用程序时,这是我获得的 logcat:
> 10-17 08:32:11.241 16762-16762/obx.com.futurister E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: obx.com.futurister, PID: 16762
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { }} to activity {obx.com.futurister/obx.com.futurister.OptionChooser}: java.lang.RuntimeException: Failure from system
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Failure from system
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1514)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at obx.com.futurister.OptionChooser.onActivityResult(OptionChooser.java:75)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.os.TransactionTooLargeException: data parcel size 4915644 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:503)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2657)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1507)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at obx.com.futurister.OptionChooser.onActivityResult(OptionChooser.java:75)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
类似的question ,该解决方案建议使用图像加载库,我应该这样做,还是可以轻松修复?寻求专业答案。谢谢
最佳答案
日志中已经提供了根本原因:
Caused by: android.os.TransactionTooLargeException: data parcel size 4915644 bytes
Intent 传输数据的最大上限为 1 MB,因此有几种方法可以传递位图:
- 减小位图大小,这可能是也可能不是有效的解决方案,具体取决于您的用例。
- 将用于运输的位图切碎并在接收端重新组装,您可能需要编写 service为此,它的效率不是很高。做过一次,不推荐。
- 只传递位图的 URI,并在接收 Activity 上重新加载它。这就是 Intent 要求 Android 相机应用拍照的工作原理 - 图片被保存到存储中,并且只返回存储文件的 URI。
- 如果第一个和第二个 Activity 在同一个进程中,您可以跳过以上所有步骤并将位图保存到共享缓存中,其中有许多库可以完成此操作。
关于android - 将位图传递给另一个 Activity 以 RunTimeException 结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33182309/