How to pass an object containing a bitmap to another activity
So I’m new to Android development, and I’m trying to create a registered activity (UserPrivateInfoSignUpActivity.java
) to load a picture (Bitmap) and create a UserPersona
object (which I want to pass to another activity.) UserGeneralDataSignUpActivity
)。
I
researched the solution and I still can’t pass the Bitmap image even though it already implements Parcelable
.
Any suggestions?
Here is my code (without getters and 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 (where I want to retrieve user objects).
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)
}
It successfully moves to the next Activty, but every time I load the picture and then try to move to the next activity, my app crashes.
Here I add the LogCat (after adding image)
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)
Thank you.
Solution
Passing bitmaps between two activities is not a good idea. When you try to do this, you get a TransactionTooLargeException
. The maximum limit for transactional data is around 1MB, and Bitmap can easily exceed it. This can cause a crash.
You can use the URI you got in onActivityResult()
with the following code
Uri selectedImage = data.getData();
Pass this URI between activities. You can then load the image with the URI on demand instead of transferring the entire Bitmap object.
@SabaJafarzade Thank you for pointing out the reuse URI.