2015-04-01 40 views
0

我需要啓動一個線程從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(); 

} 
+0

如果重寫'onActivityResult'? – 2015-04-01 01:10:32

+0

我已經添加了它,但onActivityResult不是問題。我在DownloadImage類中爲我的意圖數據獲取null。 – 2015-04-01 01:30:04

回答

0

除了創建你自己Runnable,我建議你使用Android AsyncTask從網絡上下載圖像。

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 

private class DownloadImage extends AsyncTask<Void, Void, Void> { 

    ProgressDialog pdLoading = new ProgressDialog(DownloadImage.this); 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     //this method will be running on UI thread 
     pdLoading.setMessage("\tDownloading..."); 
     pdLoading.show(); 
    } 
    @Override 
    protected Void doInBackground(Void... params) { 
     //can access params like params[1], params[2] ... 
     //this method will be running on background thread so don't update UI frome here 
     //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here 


     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 

     //this method will be running on UI thread 

     pdLoading.dismiss(); 
    } 

}

則可以執行類似new DownloadImage().execute();注意的AsyncTask,你可以通過你喜歡這裏的執行任何參數();詳細瞭解它Android Documentation

+0

我很想去但是這是一個類,我需要使用Handler,Message,Runnable線程方案來實現這一點。 – 2015-04-01 03:11:03

0

您必須調用startActivityForResult的意圖,並實現onActivityResult捕獲來自DownloadImage的信息。

在活動MainActivity你忘了此行:

super.onActivityResult(requestCode, resultCode, data); 

見例如:

startActivityForResult(new Intent(this, DownloadImage.class), 1); 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if(resultCode == RESULT_OK) { 
       // do something 
    } 
      super.onActivityResult(requestCode, resultCode, data); 
     } 

對於發送信息到MainActivity必須調用的setResult在你的包中的信息,並調用finish( )。

活動DownloadImage:

Bundle bundle = new Bundle(); 
    bundle.putString("---your info---"); 

    setResult(RESULT_OK, new Intent().putExtras(bundle)); 
finish(); 

閱讀節 「啓動活動和獲取結果」 從http://developer.android.com/reference/android/app/Activity.html

+0

如果我的包不是字符串,該怎麼辦?我正在使用Uri數據,而不是字符串。最後的Uri data = getIntent()。getData();命令給我一個null。 – 2015-04-01 03:59:58