我正在开发一个自动发送大量电子邮件的应用程序,但我无法获得允许用户选择文件开始的活动。代码中的一切似乎都是正确的,但是当我逐步执行说明时,似乎该活动根本没有开始。这是我的代码:
通话活动,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 文件选择器
下面仅发布扩展文件选择器的类的相关方法:
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 活动并从中断处继续。我唯一有可能选择文件的迹象是,一旦抛出并记录了异常并且代码在完成()之后暂停。此时,会打开一个弹出窗口,要求选择一个文件选择程序(应该发生的是自动使用内置文件选择器)。
如果有人可以帮助我了解发生了什么以及为什么 FileChooser 活动没有被首先调用,我将不胜感激。我找到了很多关于 OnActivityResult() 问题的资源,但不幸的是,它甚至没有走那么远。谢谢你的帮助!