我正在開發一個應用程序,可以自動發送一堆電子郵件,而且我無法獲得允許用戶選擇文件來啓動的活動。代碼中的一切似乎都是正確的,但是當我逐步執行指令時,似乎活動從未開始。這裏是我的代碼:Android StartActivityForResult未啓動該活動?
呼叫活動,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()的問題的資源,但不幸的是它甚至沒有那麼多。謝謝你的幫助!
已修復,但我仍然有同樣的問題。執行進入第二個活動類沒有例外,也沒有意義。 – LegoMyth