0

I have implemented a barcode scanner in my Android Application. I want to set the barcode image (bitmap) generated by my scanner to an ImageView in an XML file.

However, this XML file will not be initialized before I start the MatasActivity (where the Image/ImageView will be showed). This gives me a nullpointerexception, since the "imageCode" variable cannot find "R.id.imageCode" from the XML file.

How can I set the ImageView from an XML file in a new activity -- I can't do it after startActivity (since the rest of the code will not run but rather the onCreate in the new Activity)

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if(result != null) {
                if(result.getContents() == null) {
                    Log.d("MainActivity", "Cancelled scan");
                    Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
                } else {
                    Log.d("MainActivity", "Scanned");
                    Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();



Intent intent = new Intent(MainActivity.this, MatasActivity.class);
                    imageCode = (ImageView) findViewById(R.id.imageCode);
                    Bitmap myBitmap = BitmapFactory.decodeFile(result.getBarcodeImagePath());
                    imageCode.setImageBitmap(myBitmap);

                    startActivity(intent);

                }
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

The MatasActivity just have the purpose of running the XML file (to show the barcode) AKA the onCreate in MatasActivity method only performs:

    setContentView(R.layout.picked_card);

Which is the XML file where imageCode is kept.

m_h
  • 485
  • 5
  • 11

2 Answers2

0

Why do you want to set the image before starting the activity? It does not work like that. You can pass 'myBitmap' to the new Activity (MatasActivity) and then set it once it is launched.

user3793589
  • 418
  • 3
  • 14
  • I thought about that -- but I felt that it was a bit odd, especially because I need to pass a String as well. But I'll give it a try then. Cheers. – m_h Jun 26 '16 at 18:47
  • I do not think it is odd. You can pass 100 paramaters (if the memory permits it) to a new activity. You can just send 'myBitmap' as an Object and then handle it as a 'Bitmap' again in the new Activity. Yes, please try and let me know. – user3793589 Jun 26 '16 at 18:50
  • I had problems sending it as a bitmap, so I tried converting it to a bytearray first this way: http://stackoverflow.com/questions/11010386/send-bitmap-using-intent-android – m_h Jun 26 '16 at 19:01
  • Great, did it work ? Did you tried to send it as an Object too? – user3793589 Jun 26 '16 at 19:04
  • Answered you above! – m_h Jun 26 '16 at 19:26
0

Well, it partially works. Now my MatasActivity looks like this:

public class MatasActivity extends AppCompatActivity {
ImageView imageCode;
TextView imageCodeNumber;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picked_card);
    String message = "";

    try {
        byte[] byteArray = getIntent().getByteArrayExtra("image");
        Bitmap myBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

        String number = getIntent().getExtras().getString("number");

        imageCode = (ImageView) findViewById(R.id.imageCode);
        imageCode.setImageBitmap(myBitmap);

        imageCodeNumber = (TextView) findViewById(R.id.imageCodeNumber);
        imageCodeNumber.setText(number);
    }
    catch (Exception e) {
        message = e.getMessage();
    }
}

However, if I try to access the Activity from another part of the application, it obviously won't display the barcode picture.

Also, I was a bit unsure as to how I should pass it as an object?

m_h
  • 485
  • 5
  • 11
  • Of course it won't work since what happened here is that 'MainActivity' gave the image to 'MatasActivity' and told it : "Continue the work". For it to work from another part of the application, 'Another part of the aplication' needs to give 'MatasActivity' an image as well in its parameters – user3793589 Jun 26 '16 at 20:42
  • Yes, exactly. However, the picture is generated from the onActivityResult method in the MainActivity -- so the picture cannot be (and shouldn't be) generated from another part of the application. – m_h Jun 27 '16 at 10:24
  • Indeed... Unless you have another 'onActivityResult method' in 'another part of the application'. What is exactly your flow? You can create it as another question. If you think this thread's main question is answered, please accept it. – user3793589 Jun 28 '16 at 08:02