将位图传递给另一个活动以RunTimeException结尾 [英] Passing Bitmap to Another activity ends in RunTimeException
问题描述
我正在尝试将Bitmap传递给另一个Activity,并且正在使用ImageView
从另一个Activity显示相同的图像.这就是我通过位图的方式.
I am trying to pass Bitmap to another Activity, and I am displaying the same image from the other Activity using ImageView
. And this is how I pass the Bitmap.
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);
这就是我接收值的方式:
And this is how I receive the value :
Intent intent = getIntent();
bitmap= (Bitmap)intent.getParcelableExtra("BitmapImage");
ImageView im1 = (ImageView)findViewById(R.id.camOut);
im1.setImageBitmap(bitmap);
在运行该应用程序时,这是我获得的日志:
And while running the App, here is the logcat I obtain :
> 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)
在类似的问题,该解决方案建议使用图像加载库,我应该这样做,还是可以轻松解决此问题?寻找专业的答案.谢谢
In a similar question , the solution recommends to use image loading libraries, should I go for that, or can this be fixed easily? Looking for professional answers. Thanks
推荐答案
已在日志中提供了根本原因:
The root cause has been provided in the log:
由以下原因引起:android.os.TransactionTooLargeException:数据包大小 4915644字节
Caused by: android.os.TransactionTooLargeException: data parcel size 4915644 bytes
通过Intent进行数据传输的最大上限为1 MB,因此有几种传递位图的方法:
The maximum cap for data transport by Intent is 1 MB, so there are a few ways to pass a bitmap:
- 减小位图的大小,根据您的使用情况,这可能是有效的解决方案,也可能不是有效的解决方案.
- 将位图切碎并在接收端重新组装,您可能需要编写服务,效率并不高.做过一次,不推荐.
- 仅传递位图的URI,然后将其重新加载到接收方的Activity上.这就是Intent要求Android相机应用拍摄照片的方式-将照片保存到存储中,仅返回存储文件的URI.
- 如果第一个活动和第二个活动处于相同的过程中,则可以跳过以上所有内容,并将位图保存到共享的缓存中,那里有很多很多的库来完成该工作.
- Reduce bitmap size,this may or may not be a valid solution depending on your use case.
- Chop up the bitmap for transportation and reassembles it on the receiving end, you'll probably need to write a service for it, and it's not terribly efficient. Did it once, not recommended.
- Only passing the URI for the bitmap, and load it anew on the receiving Activity. This is how Intent asking Android camera app to take a picture works - the picture is saved to storage and only the URI of the stored file is returned.
- If the 1st and 2nd Activities are in the same process, you can skip all of above and save the bitmap to a shared cache, where there are many, many libraries to accomplish that.
这篇关于将位图传递给另一个活动以RunTimeException结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!