我需要啓動一個線程從Web下載圖像,然後通過意圖將該圖像在外部驅動器上的位置返回給調用活動。在這個例子中,我從MainActivity開始,創建一個調用可運行和線程所在的DownloadImage類的意圖。該可運行的程序依次調用一個實用程序類來完成UI線程的所有工作。如何讓意向數據從可運行的回到呼叫意圖
我卡在哪裏是如何將工作結果返回到MainActivity。當我運行這段代碼時,我的意圖數據得到一個空值。誰能告訴我問題在哪裏?
---------------------------------------------------------
MainActivity.class
...
Uri urlData = Uri.parse("http://www.testUrl.com/image.png");
Intent downloadIntent = new Intent(this, DownloadImage.class);
downloadIntent.putExtra("imageUri", urlData);
startActivityForResult(downloadIntent, DOWNLOAD_IMAGE_REQUEST);
@Override
protected void onActivityResult(int requestCode,
int resultCode,
Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == DOWNLOAD_IMAGE_REQUEST) {
Intent galleryIntent = makeGalleryIntent(data.toString());
startActivity(galleryIntent);
}
}
else if (resultCode != RESULT_OK || requestCode != DOWNLOAD_IMAGE_REQUEST) {
Toast.makeText(this,
"Activity did not complete correctly.",
Toast.LENGTH_SHORT).show();
}
}
---------------------------------------------------------
DownloadImage.class
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Uri data = getIntent().getData();
Runnable downloadRunnable = new Runnable() {
@Override
public void run() {
Intent i = new Intent(DownloadImage.this, MainActivity.class)
i.putExtra("urlData", DownloadUtils.downloadImage(DownloadImageActivity.this, data));
}
// downloadImage takes in a Uri object and context to create a location on disk for the result. returns a uri object.
setResult(RESULT_OK, i);
};
thread = new Thread (downloadRunnable);
thread.start();
finish();
}
如果重寫'onActivityResult'? – 2015-04-01 01:10:32
我已經添加了它,但onActivityResult不是問題。我在DownloadImage類中爲我的意圖數據獲取null。 – 2015-04-01 01:30:04