简体   繁体   中英

Integrate git project on an existing project startActivityForResult and onActivityResult android

I implemented this git on my current project without cloning

implementation 'com.github.adityaarora1:LiveEdgeDetection:master-SNAPSHOT'

But I'm unable to call it on my method. The document says

  1. Start startActivityForResult from your activity

     startActivityForResult(new Intent(this, ScanActivity.class), REQUEST_CODE);
  2. Get a file path for cropped image on onActivityResult

    String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT); Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);

So I tried calling like this onClick button from a new Class

   scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(MainActivity.this,
                        ScanActivity.class);
                startActivity(myIntent);
            }

and put the rest inside my onActivityResult

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

    String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
    Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
}

Edit : here is the MainActivity the author used on git I tried using it I get this error:

scannedImageView = findViewById(com.adityaarora.liveedgedetection.R.id.scanned_image);

MainActivity (imported)

private static final int REQUEST_CODE = 101;
private ImageView scannedImageView;
Button scan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan);
    scannedImageView = findViewById(com.adityaarora.liveedgedetection.R.id.scanned_image);
    startScan();

    scan = findViewById(R.id.open_scan);
        scan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(ScanActivity2.this,
                    ScanActivity.class);
            startActivityForResult(myIntent ,111);
        }
    });
}

private void startScan() {
    Intent intent = new Intent(this, ScanActivity.class);
    startActivityForResult(intent, REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE) {
        if(resultCode == Activity.RESULT_OK) {
            if(null != data && null != data.getExtras()) {
                String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
                Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
                scannedImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                scannedImageView.setImageBitmap(baseBitmap);
            }
        } else if(resultCode == Activity.RESULT_CANCELED) {
            finish();
        }
    }
}

Update :

After some research I found that the imported project was on read file only and cannot be changed (ScanActivity.java) and my current project was updated sdk 28 which is different from the one Imported so there is some errors in ScanActivity which Is why the button (technically) wasn't working

You should use startActivityForResult instead of startActivity like below.

scan.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         Intent myIntent = new Intent(MainActivity.this,
                        ScanActivity.class);
         startActivityForResult(myIntent ,111);
    }
});

and modify your onActivityResult like

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

    if(resultCode == Activity.RESULT_OK && requestCode == 111){
         String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
         Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
         Log.d("YourTAG","File Path "+filePath);
         // here you can set bitmap to your image view 
         yourImageView.setImageBitmap(baseBitmap);
    }
}

UPDATE

You Main Activity should be like

private static final int REQUEST_CODE = 111;
private ImageView scannedImageView;
Button scan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan);
    scannedImageView = findViewById(R.id.scanned_image); // this  ImageView should be in your activity_scan.xml file with same id(scanned_image)
    startScan();

    scan = findViewById(R.id.open_scan);
    scan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           startScan();
        }
    });
}

private void startScan() {
    Intent intent = new Intent(ScanActivity2.this, ScanActivity.class);
    startActivityForResult(intent, REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE) {
        if(resultCode == Activity.RESULT_OK) {
            if(null != data && null != data.getExtras()) {
                String filePath = data.getExtras().getString(ScanConstants.SCANNED_RESULT);
                Bitmap baseBitmap = ScanUtils.decodeBitmapFromFile(filePath, ScanConstants.IMAGE_NAME);
                scannedImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                scannedImageView.setImageBitmap(baseBitmap);
            }
        } else if(resultCode == Activity.RESULT_CANCELED) {
            finish();
        }
    }
}

Demo project

I have uploaded a demo project on Github which is integrated LiveEdgeDetection library and working as expected. To check it go here

try this..

scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent i = new Intent(this, ScanActivity.class);
               startActivityForResult(i, REQUEST_CODE);
            }

and put onActivityResult in this class which is give you a result from ScanActivity Class..

  @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == REQUEST_CODE) {
                if(resultCode == Activity.RESULT_OK) {
                    if(null != data && null != data.getExtras()) {
                     // here is your result
                    }
                } else if(resultCode == Activity.RESULT_CANCELED) {
                    finish();
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM