Search code examples
androidandroid-emulatorandroid-galleryandroid-image

When pressed back button, i got Force Close in Android


in my activity, i can go to gallery and can pick image. After that i can go back to previous sreecn. But when i went to gallery, if i wouldn't pick an image and press to back button, i can not go to previous screen and i got force close. How can fix that, without using startActivity(intent) ? Here is my code : first i defined to

private static final int ACTIVITY_REQUEST_PICK_ATTACHMENT = 1;

On Activity Result Code:

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




    super.onActivityResult(requestCode, resultCode, data);

            mAttachments = (LinearLayout) findViewById(R.id.attachments);

            switch (requestCode) {
            case ACTIVITY_REQUEST_PICK_ATTACHMENT:

                Uri _uri = data.getData();

                addAttachment(_uri);

                Cursor cursor = getContentResolver()
                        .query(_uri,
                                new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
                                null, null, null);
                cursor.moveToFirst();
                File imageFilePath = new File(cursor.getString(0));
                uris.add(imageFilePath);
                names.add(imageFilePath.getName());

                Log.v("imageFilePath", imageFilePath.toString());
                break;

i call the that at here:

private void onAddAttachment2(final String mime_type) {

        // setContentView(R.layout.main);

        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType(mime_type);
        startActivityForResult(Intent.createChooser(i, null),
                ACTIVITY_REQUEST_PICK_ATTACHMENT);
    }

the error on my LogCat

05-20 13:16:39.809: E/AndroidRuntime(374):  at my.mail.SenderActivity.onActivityResult(KepenekActivity.java:294)

when i double click to error, it shows the line

Uri _uri = data.getData();

logically it is true, my _uri is empty, how can i show the previous screen with this final state here is my problem.


Solution

  • You need to add a check for the resultcode.

    protected void onActivityResult(int requestCode, int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        mAttachments = (LinearLayout) findViewById(R.id.attachments);
    
        switch (requestCode) {
            case ACTIVITY_REQUEST_PICK_ATTACHMENT:
                if (resultCode == RESULT_OK) { // <------ THIS LINE IS IMPORTANT
                    Uri _uri = data.getData();
    
                    addAttachment(_uri);
    
                    Cursor cursor = getContentResolver()
                        .query(_uri,
                                new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
                                null, null, null);
                    cursor.moveToFirst();
                    File imageFilePath = new File(cursor.getString(0));
                    uris.add(imageFilePath);
                    names.add(imageFilePath.getName());
    
                    Log.v("imageFilePath", imageFilePath.toString());
                }
                break;
    

    If you press the back button instead of choosing something the resultCode will get set to RESULT_CANCELLED instead of RESULT_OK. You can use that distinction to do whatever you need to in either case.