JavaBinder FAILED BINDER TRANSACTION

3.9k views Asked by At

I am getting this error in logcat when i try to upload image grom gallery. when i upload image from camera directly it works fine. Below is my code

public class OfflineMerchantRegisterActivity extends AppCompatActivity {

EditText edtUsername, edtPassword, edtConfirmPassword, edtEmail, edtMobile, edtServiceType, edtName, edtAddress, edtCity;
Button btnRegister, btnSelectImage, btnSelectCity, btnSelectType;
ImageView imgMerchant;
String offline_service_type;
LinearLayout layoutCity, layoutServiceType;
EditText edtService;
String service;

String username, password, confirmPassword, email, mobile, serviceType, name, address, city;

Context context;
private String userChoosenTask;

private static final int CAMERA_REQUEST = 1888;
private static final int GALLERY_REQUEST = 2;

Bitmap imageBitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_offline_merchant_register);

    context=OfflineMerchantRegisterActivity.this;

    edtUsername = (EditText) findViewById(R.id.edtUsername);
    edtPassword = (EditText) findViewById(R.id.edtPassword);
    edtConfirmPassword = (EditText) findViewById(R.id.edtConfirmPassword);
    edtEmail = (EditText) findViewById(R.id.edtEmail);
    edtMobile = (EditText) findViewById(R.id.edtMobile);
    edtServiceType = (EditText) findViewById(R.id.edtServiceType);
    edtName = (EditText) findViewById(R.id.edtName);
    edtAddress = (EditText) findViewById(R.id.edtAddress);
    edtCity = (EditText) findViewById(R.id.edtCity);
    edtService = (EditText) findViewById(R.id.edtService);
    edtService.setText("Offline Merchant");

    btnRegister = (Button) findViewById(R.id.btnRegister);
    btnSelectImage = (Button) findViewById(R.id.btnSelectImage);
    btnSelectCity = (Button) findViewById(R.id.btnSelectCity);
    btnSelectType = (Button) findViewById(R.id.btnSelectType);

    imgMerchant = (ImageView) findViewById(R.id.imgMerchant);

    layoutCity = (LinearLayout) findViewById(R.id.layoutCity);
    layoutServiceType = (LinearLayout) findViewById(R.id.layoutServiceType);

    btnSelectCity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(OfflineMerchantRegisterActivity.this, MerchantCityActivity.class);
            startActivityForResult(intent, 3);
        }
    });

    btnSelectType.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(OfflineMerchantRegisterActivity.this, OfflineServiceTypeListActivity.class);
            startActivityForResult(intent, 1);
        }
    });

    layoutServiceType.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    layoutCity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isValid()){
                //registerOfflineMerchant();
                Intent intent = new Intent(OfflineMerchantRegisterActivity.this, OfflineMerchantConfirmActivity.class);
                intent.putExtra("username", edtUsername.getText().toString());
                intent.putExtra("password", edtPassword.getText().toString());
                intent.putExtra("email", edtEmail.getText().toString());
                intent.putExtra("mobile", edtMobile.getText().toString());
                intent.putExtra("serviceType", edtServiceType.getText().toString());
                intent.putExtra("name", edtName.getText().toString());
                intent.putExtra("address", edtAddress.getText().toString());
                intent.putExtra("city", edtCity.getText().toString());

                if (imageBitmap!=null){
                    intent.putExtra("imageString", getStringImage(imageBitmap));
                    intent.putExtra("fileName", username+".png");
                }
                else {
                    //do nothing
                }
                intent.putExtra("service", edtService.getText().toString());

                startActivity(intent);
            }
        }
    });

    btnSelectImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           /* Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, 2);*/
            selectImage();
        }
    });
}

public boolean isValidMail(String email) {
    // TODO Auto-generated method stub
    boolean isValid = false;

    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    CharSequence inputStr = email;

    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}

public boolean isValid(){

    username = edtUsername.getText().toString().trim();
    name = edtName.getText().toString().trim();
    password = edtPassword.getText().toString().trim();
    confirmPassword = edtConfirmPassword.getText().toString().trim();
    email = edtEmail.getText().toString().trim();
    mobile = edtMobile.getText().toString().trim();
    serviceType = edtServiceType.getText().toString().trim();
    address = edtAddress.getText().toString().trim();
    city = edtCity.getText().toString().trim();
    service = edtService.getText().toString().trim();

    if (username.length() <=0){
        Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Username", Toast.LENGTH_SHORT).show();
        return false;
    }

    if (name.length() <=0){
        Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Name", Toast.LENGTH_SHORT).show();
        return false;
    }

    if (password.length() <=0){
        Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Password", Toast.LENGTH_SHORT).show();
        return false;
    }

    if (confirmPassword.length() <=0){
        Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Password Again", Toast.LENGTH_SHORT).show();
        return false;
    }

    if (!(password.equals(confirmPassword))) {
        Toast.makeText(this, "Passwords Mismatch", Toast.LENGTH_SHORT).show();
        return false;
    }

    if (email.length() <=0){
        Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Email", Toast.LENGTH_SHORT).show();
        return false;
    }

    if (!isValidMail(email)) {
        Toast toast = Toast.makeText(this, "Please Enter Valid Email", Toast.LENGTH_SHORT);
        toast.show();
        return false;
    }

    if (mobile.length() <=9){
        Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Mobile Number", Toast.LENGTH_SHORT).show();
        return false;
    }

    if (serviceType.length() <=0){
        Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Select Service Type", Toast.LENGTH_SHORT).show();
        return false;
    }

    if (address.length() <=0){
        Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Address", Toast.LENGTH_SHORT).show();
        return false;
    }

    if (city.length() <=0){
        Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Select City", Toast.LENGTH_SHORT).show();
        return false;
    }

    if (service.length() <= 0){
        Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter As Offline Service", Toast.LENGTH_SHORT).show();
        return false;
    }

    return true;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 1) {
            offline_service_type = data.getStringExtra("offlineServiceType");
            edtServiceType.setText(offline_service_type);
        }

        if (resultCode == Activity.RESULT_OK){
            if (requestCode == 3){
                city = data.getStringExtra("city");
                edtCity.setText(city);
            }
        }


        if (resultCode == RESULT_OK && null != data) {

            if (requestCode == GALLERY_REQUEST) {
                Uri URI = data.getData();
                String[] FILE = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(URI, FILE, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(FILE[0]);
                String imageDecode = cursor.getString(columnIndex);
                cursor.close();
                imageBitmap = BitmapFactory.decodeFile(imageDecode);
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                imgMerchant.setImageBitmap(imageBitmap);
                //uploadImageToServer(getStringImage(bmp), username);

            } else if (requestCode == CAMERA_REQUEST) {
                imageBitmap = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

                File destination = new File(Environment.getExternalStorageDirectory(),
                        System.currentTimeMillis() + ".jpg");

                FileOutputStream fo;
                try {
                    destination.createNewFile();
                    fo = new FileOutputStream(destination);
                    fo.write(bytes.toByteArray());
                    fo.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                imgMerchant.setImageBitmap(imageBitmap);
                //uploadImageToServer(getStringImage(thumbnail), username);
            }
        }
    }
}


public String getStringImage(Bitmap bmp) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
    byte[] imageBytes = outputStream.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (userChoosenTask.equals("Take Photo"))
                    cameraIntent();
                else if (userChoosenTask.equals("Choose from Library"))
                    galleryIntent();
            } else {
                //code for deny
            }
            break;
    }
}


private void selectImage() {
    final CharSequence[] items = {"Take Photo", "Choose from Library",
            "Cancel"};

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Add Photo");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            boolean result = Utility.checkPermission(context);
            if (items[item].equals("Take Photo")) {
                userChoosenTask = "Take Photo";
                if (result)
                    cameraIntent();

            } else if (items[item].equals("Choose from Library")) {
                userChoosenTask = "Choose from Library";
                if (result)
                    galleryIntent();

            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}


private void galleryIntent() {

    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(intent, GALLERY_REQUEST);

    //startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}

private void cameraIntent() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, CAMERA_REQUEST);

}


public void onBackPressed() {
    OfflineMerchantRegisterActivity.this.finish();
    super.onBackPressed();

}

}

How to solve this error ? after selecting image from gallery, app crash error.

1

There are 1 answers

2
Samvid Mistry On BEST ANSWER

This error mostly occurs when the data you are filling in the intent is more than the system is able to transfer through Intent bundle. Check to see the size of all the things you are putting in the bundle. And also check to see that the image you are selecting from gallery is not very large, as the system may not be able to transfer the image from gallery to you app. The camera code is working because the camera app transfers a low-resolution image through the Intent.