How to access onActivityResult () member outside of it in any other Android function

This is my oncreate method with camera image capture logic: -

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_driverregister);
    app = (MyApplication) getApplication();
    btnTackPic = (Button) findViewById(R.id.btnTakePic);
    driverPhoto = (ImageView) findViewById(R.id.ivThumbnailPhoto);
    driverName = (EditText) findViewById(R.id.driverName);
    address = (EditText) findViewById(R.id.address);
    contactNumber = (EditText) findViewById(R.id.contactNumber);
    licenseNumber = (EditText) findViewById(R.id.licenseNumber);


    btnTackPic.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // create intent with ACTION_IMAGE_CAPTURE action
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // to save picture remove comment
    File file = new File(Environment.getExternalStorageDirectory(),
    "my-photo.jpg");
    Uri photoPath = Uri.fromFile(file);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);
    intent.putExtra("data", true);
    setResult(1, intent);
    // start camera activity
    startActivityForResult(intent, TAKE_PICTURE);


    System.out.println("driverName: "+driverName.getText());
    System.out.println("Address: "+address.getText());
    System.out.println("String is"+encodedImage);
    System.out.println("photo path is"+photoPath);

}

This is the onActivityResult () function, where I write the logic to convert the image to base64 and then to a string. I want to publish in application / json format along with other form fields in my form submission part. When trying to print encodedImageit displays a zero every time, but it will be printed in onActivityResult(), which is below: -

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent != null){
        // get bundle
        Bundle extras = intent.getExtras();

        // get
        bitMap = (Bitmap) extras.get("data");

        driverPhoto.setImageBitmap(bitMap);
         /* Code for Image */
        Bitmap bitmap = ((BitmapDrawable)driverPhoto.getDrawable()).getBitmap();
        ByteArrayOutputStream stream=new ByteArrayOutputStream();

        bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
        byte[] image=stream.toByteArray();
        System.out.println("byte array:"+image);
        String encodedImage = Base64.encodeToString(image, Base64.DEFAULT);
       // img_str = Base64.encodeToString(image, 0);
       // System.out.println("string:"+img_str);
       // app.setImgString(img_str);
        System.out.println("string:"+encodedImage);
    }
}

I want to access this encodedImage beyond and use it as a global variable so that I can send messages in JSON format instead of multipart.

0
1

?

onActivityResult :

Intent intent = new Intent();
intent.setAction("ACTIVITY_RESULT");
intent.putExtra("requestCode", requestCode);
intent.putExtra("resultCode", resultCode);
intent.putExtra("encodedImage", encodedImage);
getBaseContext().sendBroadcast(intent);

:

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
     // stufs...
     IntentFilter intentFilter = new  IntentFilter("ACTIVITY_RESULT");
     this.registerReceiver(mReceiver, intentFilter);
     // other stufs...
}


//The BroadcastReceiver that listens for Notification broadcasts
public final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) 
{
    final String action = intent.getAction();
    // get notification action
    if (action.equals("ACTIVITY_RESULT")) {
        // get encoded image extra parameter 
        String encodedImage = intent.getStringExtra("encodedImage");
        // get others parameters...
    }
}

, , Broadcast Receivers, .

0

All Articles