我正在开发一个可自动执行大量电子邮件发送的应用程序,但我无法获得允许用户选择一个文件来完全启动文件的活动。代码中的所有内容似乎都是正确的,但是当我逐步执行说明时,似乎该活动根本没有开始。这是我的代码:

呼叫活动,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 = "";
        }
    }
}


文件选择器来自以下问题中发布的库:Android file chooser

下面仅发布扩展文件选择器的类的相关方法:

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();
        }
    }
}


最后,这是清单中声明被调用类的代码段:

<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>


在EmailSender尝试附加空文件名之前,我在Logcat中没有任何异常。调试器几乎只是逐步浏览Android API中的指令,直到返回到EmailSender活动,并从中断处继续。我唯一有可能选择一个文件的提示是,在throw()之后引发了异常并记录了异常并且代码已暂停。此时,将打开一个弹出窗口,要求选择文件选择程序(应该发生的是自动使用内置文件选择器)。

如果有人可以帮助我了解发生了什么以及为什么不首先调用FileChooser活动,我将非常感激。我发现了很多关于OnActivityResult()问题的资源,但不幸的是,它甚至还没有解决。谢谢你的帮助!

最佳答案

嗯,在您的try catch块之后,您即将完成活动。
删除finish()行,以便您可以startActivityForResult并返回一些内容。

10-08 14:09