如何通过 AsyncTask 正确使用 Intent 和 Uri 数据
How to properly use Intent and Uri data with AsyncTask
我需要使用 AsyncTask 从 Web 下载图像。我想使用可以返回到我的 MainActivity 的意图来执行此操作。下面的代码确实下载了图像,但是我必须按一次后退按钮才能真正看到图像。我传入的 Uri 是下载方法正常工作所需的数据。
这是我的问题:
如何将 Uri 传递到 AsyncTask,正确形成启动下载方法的意图,然后 return 以该意图调用 activity?
另外请不要提Picasso,我特别需要用到AsyncTask
private class DownloadAsyncTask extends AsyncTask<Uri, Void, Intent>{
private Uri mUri;
public DownloadAsyncTask(Uri uri) {
this.mUri = uri;
}
@Override
protected void onPreExecute() {
Toast.makeText(DownloadActivity.this, "Beginning download...", Toast.LENGTH_SHORT).show();
};
@Override
protected Intent doInBackground(Uri... params) {
Log.i(TAG, "in doInBackground()");
Intent i = new Intent(DownloadActivity.this, MainActivity.class);
//set the intent data to the result of the method that actually downloads this image
i.setData(DownloadTools.downloadImage(DownloadActivity.this, mUri));
setResult(RESULT_OK, i);
return i;
}
protected void onPostExecute() {
Toast.makeText(DownloadImageActivity.this, "Finished Download.", Toast.LENGTH_SHORT).show();
finish();
};
}
MainActivity 回调:
@Override
protected void onActivityResult(int requestCode,
int resultCode,
if (resultCode == RESULT_OK) {
if (requestCode == DOWNLOAD_IMAGE_REQUEST) {
//use the path found in the data to open the image in the gallery.
String address = data.getData().toString();
Intent galleryIntent = makeGalleryIntent(address);
// Start the Gallery Activity.
startActivity(galleryIntent);
}
}
您为什么尝试使用意图来执行此方法?
据我了解,您想启动 AynTask 并将结果传回主要活动,不是吗?为此,您应该使用和接口来实现回调。
public interface I_CallBackTask {
public void getResultFromAsynTask(String result);
}
private class DownloadAsyncTask extends AsyncTask<Uri, Void, String>{
private Uri mUri;
public I_CallBackTask callback;
public DownloadAsyncTask(Uri uri) {
this.mUri = uri;
}
@Override
protected void onPreExecute() {
Toast.makeText(DownloadActivity.this, "Beginning download...", Toast.LENGTH_SHORT).show();
};
@Override
protected String doInBackground(Uri... params) {
//Do your background code for download here
//return the result of your download: if is an image it could be byte[], ...
return resultfrombackground;
}
//result same type as resultfrombackground
protected void onPostExecute(String result) {
callback.getResultFromAsynTask(result);
};
}
//your main activity
public YourActivity implements CallBackTask{
// your Activity
//your code .....
//On your main activity start the asyntask and pass your uri in the parameter
DownloadAsyncTask asyntask = new DownloadAsyncTask(yourUri);
asyncTask.execute();
asyncTask.callback = this;
@Override
public void getResultFromAsynTask(String result){
// do what you need with the result and start the new activity
}
}
我需要使用 AsyncTask 从 Web 下载图像。我想使用可以返回到我的 MainActivity 的意图来执行此操作。下面的代码确实下载了图像,但是我必须按一次后退按钮才能真正看到图像。我传入的 Uri 是下载方法正常工作所需的数据。
这是我的问题: 如何将 Uri 传递到 AsyncTask,正确形成启动下载方法的意图,然后 return 以该意图调用 activity?
另外请不要提Picasso,我特别需要用到AsyncTask
private class DownloadAsyncTask extends AsyncTask<Uri, Void, Intent>{
private Uri mUri;
public DownloadAsyncTask(Uri uri) {
this.mUri = uri;
}
@Override
protected void onPreExecute() {
Toast.makeText(DownloadActivity.this, "Beginning download...", Toast.LENGTH_SHORT).show();
};
@Override
protected Intent doInBackground(Uri... params) {
Log.i(TAG, "in doInBackground()");
Intent i = new Intent(DownloadActivity.this, MainActivity.class);
//set the intent data to the result of the method that actually downloads this image
i.setData(DownloadTools.downloadImage(DownloadActivity.this, mUri));
setResult(RESULT_OK, i);
return i;
}
protected void onPostExecute() {
Toast.makeText(DownloadImageActivity.this, "Finished Download.", Toast.LENGTH_SHORT).show();
finish();
};
}
MainActivity 回调:
@Override
protected void onActivityResult(int requestCode,
int resultCode,
if (resultCode == RESULT_OK) {
if (requestCode == DOWNLOAD_IMAGE_REQUEST) {
//use the path found in the data to open the image in the gallery.
String address = data.getData().toString();
Intent galleryIntent = makeGalleryIntent(address);
// Start the Gallery Activity.
startActivity(galleryIntent);
}
}
您为什么尝试使用意图来执行此方法? 据我了解,您想启动 AynTask 并将结果传回主要活动,不是吗?为此,您应该使用和接口来实现回调。
public interface I_CallBackTask {
public void getResultFromAsynTask(String result);
}
private class DownloadAsyncTask extends AsyncTask<Uri, Void, String>{
private Uri mUri;
public I_CallBackTask callback;
public DownloadAsyncTask(Uri uri) {
this.mUri = uri;
}
@Override
protected void onPreExecute() {
Toast.makeText(DownloadActivity.this, "Beginning download...", Toast.LENGTH_SHORT).show();
};
@Override
protected String doInBackground(Uri... params) {
//Do your background code for download here
//return the result of your download: if is an image it could be byte[], ...
return resultfrombackground;
}
//result same type as resultfrombackground
protected void onPostExecute(String result) {
callback.getResultFromAsynTask(result);
};
}
//your main activity
public YourActivity implements CallBackTask{
// your Activity
//your code .....
//On your main activity start the asyntask and pass your uri in the parameter
DownloadAsyncTask asyntask = new DownloadAsyncTask(yourUri);
asyncTask.execute();
asyncTask.callback = this;
@Override
public void getResultFromAsynTask(String result){
// do what you need with the result and start the new activity
}
}