所以我是Android开发中的新手,我试图制作一个注册活动(UserPrivateInfoSignUpActivity.java
)来加载图片(Bitmap)并创建一个我想要的UserPersona
对象传递到另一个活动(UserGeneralDataSignUpActivity
)。
我对解决方案进行了研究,但我仍然无法传递Bitmap图像,尽管它已经是implements Parcelable
。
有什么建议吗? 这是我的代码(没有getter& setters)
PersonaUser.java:
public class PersonaUser implements Parcelable{
private String puName;
private String puEmailAddress;
private String puCountry;
private String puCity;
private int puAge;
private Bitmap puImage;
public PersonaUser(String puName, String puEmailAddress, String puCountry, String puCity,
int puAge, Bitmap puImage){
this.puName = puName;
this.puEmailAddress = puEmailAddress;
this.puCountry = puCountry;
this.puCity = puCity;
this.puAge = puAge;
this.puImage = puImage;
}
//Constructor using parcel
public PersonaUser(Parcel in) {
this.puName = in.readString();
this.puEmailAddress = in.readString();
this.puCountry = in.readString();
this.puCity = in.readString();
this.puAge = in.readInt();
this.puImage = (Bitmap) in.readValue(Bitmap.class.getClassLoader());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(puName);
parcel.writeString(puEmailAddress);
parcel.writeString(puCountry);
parcel.writeString(puCity);
parcel.writeInt(puAge);
parcel.writeValue(puImage);
}
public static final Parcelable.Creator<PersonaUser> CREATOR = new Parcelable.Creator<PersonaUser>() {
@Override
public PersonaUser createFromParcel(Parcel in) {
return new PersonaUser(in); //using parcelable constructor
}
@Override
public PersonaUser[] newArray(int i) {
return new PersonaUser[i];
}
};
}
UserPrivateInfoSignUpActivity.java:
public class UserPrivateInfoSignUpActivity extends AppCompatActivity {
private static int IMAGES_FROM_GALLERY = 1;
Button nextActivityButton, pickImageButton;
Bitmap puImageBitmap;
PersonaUser npUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_private_info_sign_up);
nextActivityButton = findViewById(R.id.next_activity);
pickImageButton = findViewById(R.id.pick_image);
addImage();
nextActivity();
}
//Get image from gallery
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
puImageBitmap = null;
//Detects request codes
if (requestCode == IMAGES_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
try {
puImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(puImageBitmap);
}
// next Activity
public void nextActivity(){
nextActivityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(UserPrivateInfoSignUpActivity.this,
UserGeneralDataSignUpActivity.class);
npUser = new PersonaUser("example", "example","example","example",21,puImageBitmap);
i.putExtra("user" , npUser);
startActivity(i);
}
});
}
//add Image method
public void addImage(){
pickImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, IMAGES_FROM_GALLERY);
}
});
}
}
UserGeneralDataSignUpActivity.java (我要检索用户对象的位置)
public class UserGeneralDataSignUpActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_general_data_sign_up);
Intent i = getIntent();
ImageView iv = findViewById(R.id.show); //where I want to display image
PersonaUser pu = i.getParcelableExtra("user");
iv.setImageBitmap(pu.getPuImage()); //PersonaUser method (returns Bitmap)
}
它成功移动到下一个Activty,虽然每次我加载图片然后尝试移动到下一个Activity我的应用程序崩溃。
04-15 10:40:09.720 20654-20654/com.rarely1gmail.personas E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 3687084)
04-15 10:40:09.721 20654-20654/com.rarely1gmail.personas D/AndroidRuntime: Shutting down VM
04-15 10:40:09.725 20654-20654/com.rarely1gmail.personas E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rarely1gmail.personas, PID: 20654
java.lang.RuntimeException: Failure from system
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1618)
at android.app.Activity.startActivityForResult(Activity.java:4487)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4445)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at android.app.Activity.startActivity(Activity.java:4806)
at android.app.Activity.startActivity(Activity.java:4774)
at com.rarely1gmail.personas.UserPrivateInfoSignUpActivity$1.onClick(UserPrivateInfoSignUpActivity.java:74)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.os.TransactionTooLargeException: data parcel size 3687084 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:764)
at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:4351)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1611)
at android.app.Activity.startActivityForResult(Activity.java:4487)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4445)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at android.app.Activity.startActivity(Activity.java:4806)
at android.app.Activity.startActivity(Activity.java:4774)
at com.rarely1gmail.personas.UserPrivateInfoSignUpActivity$1.onClick(UserPrivateInfoSignUpActivity.java:74)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
感谢。
答案 0 :(得分:3)
在两个活动之间传递Bitmap不是一个好主意。当你试图这样做时,你会得到TransactionTooLargeException
。事务数据的最大限制大约为1MB,Bitmap可能很容易超过它。这可能会导致崩溃。
您可以通过以下代码使用onActivityResult()
中获得的URI:
Uri selectedImage = data.getData();
在活动之间传递此URI。然后,您可以使用按需URI加载图像,而不是传输整个Bitmap对象。