Search code examples
javaandroidzxing

Passing scan result to other intent


I got the scan result from scan fragment but how am I able to send the result to other intent?

In My Scan Fragment

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(result != null) {
            Intent intent1 = new Intent(getActivity(), Trial.class);
            intent1.putExtra("Barcodes", "hi");
            startActivity(intent1);
        }
    }


Solution

  • Problem: IntentResult is a final class and do not implement Parcelable interface, so you cannot send it to another activity by using Intent.putExtra().

    Solution: Create your own ScanResult class which is a clone version of IntentResult.

    ScanResult.java

    public class ScanResult implements Parcelable {
    
        public String contents;
        public String formatName;
        public byte[] rawBytes;
        public Integer orientation;
        public String errorCorrectionLevel;
    
        public ScanResult(IntentResult intentResult) {
            contents = intentResult.getContents();
            formatName = intentResult.getFormatName();
            rawBytes = intentResult.getRawBytes();
            orientation = intentResult.getOrientation();
            errorCorrectionLevel = intentResult.getErrorCorrectionLevel();
        }
    
        protected ScanResult(Parcel in) {
            contents = in.readString();
            formatName = in.readString();
            rawBytes = in.createByteArray();
            long orientationInLong = in.readLong();
            orientation = orientationInLong != Long.MAX_VALUE ? (int) orientationInLong : null;
            errorCorrectionLevel = in.readString();
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(contents);
            dest.writeString(formatName);
            dest.writeByteArray(rawBytes);
            dest.writeLong(orientation != null ? orientation : Long.MAX_VALUE);
            dest.writeString(errorCorrectionLevel);
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        public static final Creator<ScanResult> CREATOR = new Creator<ScanResult>() {
            @Override
            public ScanResult createFromParcel(Parcel in) {
                return new ScanResult(in);
            }
    
            @Override
            public ScanResult[] newArray(int size) {
                return new ScanResult[size];
            }
        };
    }
    

    Using it in your fragment.

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(result != null) {
            ScanResult scanResult = new ScanResult(result);
            Intent intent1 = new Intent(getActivity(), Trial.class);
            intent1.putExtra("scanResult", scanResult);
            startActivity(intent1);
        }
    }
    

    In received activity (Trial.java)

    ScanResult scanResult = getIntent().getParcelableExtra<ScanResult>("scanResult");