我正在开发一个能够自动发送大量电子邮件的应用程序,而且我无法获得允许用户选择要启动的文件的活动。代码中的所有内容似乎都是正确的,但是当我逐步执行指令时,似乎活动根本就没有开始。这是我的代码:
调用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 = "";
}
}
}
文件选择器来自此问题中发布的库: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活动并继续它停止的地方。只有在抛出并记录异常并且代码在finish()之后暂停时,我才能指示可能选择文件的唯一一次。此时,弹出窗口打开,要求选择要选择的文件程序(应该发生的是自动使用内置文件选择器)。
如果有人能帮我理解发生了什么,以及为什么FileChooser活动没有被调用,我真的很感激。我已经在OnActivityResult()的问题上找到了很多资源,但不幸的是它甚至没有达到那么远。谢谢你的帮助!
答案 0 :(得分:1)
finish()
行,以便startActivityForResult
可以回复一些内容。