2

I'm working on an app that automates a bunch of e-mail sending, and I'm having trouble getting the activity that allows the user to select a file to start at all. Everything seems to be correct in the code, but when I'm stepping through instructions, it seems like the activity is never started at all. Here's my code:

Calling Activity, EmailSender:

public class EmailSender extends Activity{
//declarations
Intent fileIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {

    //instantiations
    String pathName;
    fileIntent = new Intent(EmailSender.this, FileChooser.class);

    //email sending functions that work fine

    try {
        GmailSender attachmentSender = new GmailSender(gsn, gpw)

        String[] toArr = new String[6];    //array of recipient addresses
        toArr[0] = efull;
        toArr[1] = afull;
        toArr[2] = ysn;
        toArr[3] = csn;
        toArr[4] = hsn;
        toArr[5] = gsn;     

        attachmentSender.setSubject("Attachment Download Test");
        attachmentSender.setFrom(gsn);
        attachmentSender.setTo(toArr);
        attachmentSender.setBody("Attachment Downloading Test");

        startActivityForResult(fileIntent, 1);
        attachmentSender.addAttachment(pathName);
        attachmentSender.send();
    }
    catch (Exception e) {
        Log.e("EmailSender", e.getMessage(), e);
    }
    finish();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == 1)
        {
            if(resultCode == RESULT_OK)
                pathName = data.getStringExtra("result");
        }
        if(resultCode == RESULT_CANCELED)
        {
            pathName = "";
        }
    }
}

The File Chooser is from the library posted in this question: Android file chooser

Only the relevant methods of the class that extends the File Chooser there are posted below:

public class FileChooser extends FileChooserActivity    {

    // TAG for log messages.
    private static final String TAG = "FileSelectorTestActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // We must check to ensure that the calling Intent is not Intent.ACTION_GET_CONTENT
        if (!isIntentGetContent()) {
            // Display the file chooser with all file types
            showFileChooser();
        }
    }

    @Override
    protected void onFileSelect(File file) {
        if (file != null) {
            //final Context context = getApplicationContext();

            // Get the path of the Selected File.
            final String path = file.getAbsolutePath();
            Log.d(TAG, "File path: " + path);

            Intent returnIntent = new Intent();
            returnIntent.putExtra("result", path);
            setResult(RESULT_OK, returnIntent);   
            finish();
        }   
    }
}

Finally, here's the snippet of my manifest where the called class is declared:

<activity
    android:name=".FileChooser"
    android:label="Choose a file"
    android:exported="false" >
        <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.OPENABLE" />

            <data android:mimeType="*/*" />
        </intent-filter>
</activity>

I do not get any exceptions in Logcat until EmailSender attempts to attach an empty filename. The debugger pretty much just steps through instructions from the Android API until it comes back to the EmailSender activity and continues where it left off. The only time I have an indication of possibly being able to select a file is once the exception has been thrown and logged and the code has paused after the finish(). At this point, a pop-up opens asking for a file choosing program to be selected (what's supposed to happen is that the built-in file chooser is used automatically).

If anyone can help me understand what's going on and why the FileChooser activity isn't being called in the first place, I'd really appreciate it. I've found a lot of resources on problems with OnActivityResult(), but unfortunately it's not even getting that far. Thanks for any help!

1 Answer 1

1

Um, right after your try catch block, you are finishing the activity. remove the finish() line so you can startActivityForResult and have something to come back to.

1
  • Fixed, but I'm still having the same problem. There's no exception and no point where execution enters the second activity class.
    – LegoMyth
    Oct 26, 2012 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.