0
votes

I implemented an app that allows the user to choose an image or take one with the camera, and once it's done, another activity will be started to present the image to the user, with some action buttons. Basically I used the Uri to pass the image, in the main activity, I have:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK)

            return;
        switch (requestCode) {
        case PHOTO_PICKED_WITH_DATA:
            // I think both cases here should start a new ac to compose a description and  then 
            // do an upload
            Uri uri = data.getData();
            appInfo = AppInfo.getInstance();
            appInfo.uri = uri;
            Log.i("MainActivity", "ready to start send activity");
            Intent intent = new Intent(this, SendActivity.class);
            startActivity(intent);

            break;
        case CAMERA_WITH_DATA:
            Uri uri_camera = data.getData();
            appInfo = AppInfo.getInstance();
            appInfo.uri = uri_camera;
            Log.i("MainActivity", "ready to start send activity");
            Intent intent_camera = new Intent(this, SendActivity.class);
            startActivity(intent_camera);

            break;
        }
    }

So when the picture is obtained, we'll get back here in onActivityResult(). I extract the uri from the intent passed in, and store it in a singleton class called AppInfo, which shared in the app so all activities can access it. Then in my second activity:

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
        }

        Log.i(LOG_TAG, "send activity created");
        ImageView imageViewPhoto = (ImageView) findViewById(R.id.imageViewPhoto);
        appInfo = AppInfo.getInstance();
        imageViewPhoto.setImageURI(appInfo.uri);

    }

So I get the uri and use it to setup the image view. But the app crashes after "send activity created". Commenting out the last three lines solves the problem. So I'm wondering is it because I cannot pass image this way? If not, how do I do it with minimum change? I mean can I still use the onActivityResult() structure to get the image taken and pass it? My phone is terrible and it does not write the stack trace to the log when the app crashes, so I don't have that info. Sorry. And many thanks in advance.

1
Can you post the error log? - Chefes

1 Answers

0
votes

without the stack trace it's hard to say, but if it's a null pointer exception either appInfo or imageViewPhoto must be null. most likely there's nothing in activity_send.xml with the id imageViewPhoto.

if it's a class cast exception then imageViewPhoto is not an ImageView.

not sure what else would go wrong with that. i believe a null uri would just clear the image.