我正在尝试将Bitmap传递给另一个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);
这就是我收到价值的方式:
Intent intent = getIntent();
bitmap= (Bitmap)intent.getParcelableExtra("BitmapImage");
ImageView im1 = (ImageView)findViewById(R.id.camOut);
im1.setImageBitmap(bitmap);
在运行App时,这是我获得的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中,解决方案建议使用图像加载库,我应该这样做,还是可以轻松修复?寻找专业的答案。感谢
答案 0 :(得分:20)
日志中提供了根本原因:
引起:android.os.TransactionTooLargeException:数据包大小 4915644字节
Intent传输数据的最大上限为1 MB,因此有几种方法可以传递位图:
答案 1 :(得分:6)
如果您可以通过以下方式捕获图像并保存到本地存储:
File out = new File(getFilesDir(), "newImage.jpg");
您应该将out
的路径传递给其他活动:
bitIntent.putExtra("BitmapImage", out.getAbsolutePath());
而不是bitIntent.putExtra("BitmapImage", mBitmap);
在secondActivity中:
Intent intent = getIntent();
String imagePath = intent.getStringExtra("BitmapImage");
File imgFile = new File(imagePath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.camOut);
myImage.setImageBitmap(myBitmap);
}
答案 2 :(得分:1)
我们无法通过意图附加内容传递Bitmap
。原因是Inner Process Communication (IPC)
总线有1mb的限制。此限制适用于代表Bundle
附加内容的Intent