2

我一直在尝试编写一个允许用户从互联网上提取图像的 android。发布此应用程序后,有人告诉我,我需要 AsyncTask 以允许我使用网络命令。我一直在研究 Android 开发者网站 (developer.android.com/reference/android/os/AsyncTask.html) 上的 AsycTask 页面。这让我完全迷失了。我不知道我需要把这段代码放在哪里。这是我的原始代码,目前正在使用,直到发布。我怎样才能在这里实现 AsyncTask ?

代码:

public void firstbutton(View view) 
    {
        InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        EditText editText = (EditText)findViewById(R.id.editText1);
        inputMgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);

        EditText idnumber=(EditText)findViewById(R.id.editText1);
        String idnumber2= idnumber.getText().toString();
        int i = Integer.parseInt(editText.getText().toString());
        idnum=i;
        setContentView(R.layout.viewer);
        Context context = view.getContext();
        Drawable image = ImageOperations(context, "HIDDEN FOR PRIVACY"+idnumber2);
        ImageView icon = new ImageView(context);
        icon = (ImageView)findViewById(R.id.imageView1);
        icon.setImageDrawable(image);
};


public Drawable ImageOperations(Context ctx, String url) {
    try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public Object fetch(String address) throws MalformedURLException,IOException
{
URL url = new URL(address);
Object content = url.getContent();
return content;
}

特瓦丁顿的 LOGCAT

11-21 20:02:30.987: E/AndroidRuntime(343): java.lang.RuntimeException: An error occured while executing doInBackground()
11-21 20:02:30.987: E/AndroidRuntime(343):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-21 20:02:30.987: E/AndroidRuntime(343):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-21 20:02:30.987: E/AndroidRuntime(343):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-21 20:02:30.987: E/AndroidRuntime(343):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-21 20:02:30.987: E/AndroidRuntime(343):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-21 20:02:30.987: E/AndroidRuntime(343):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
11-21 20:02:30.987: E/AndroidRuntime(343):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
11-21 20:02:30.987: E/AndroidRuntime(343):  at java.lang.Thread.run(Thread.java:1096)
11-21 20:02:30.987: E/AndroidRuntime(343): Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
11-21 20:02:30.987: E/AndroidRuntime(343):  at com.phil.avatarview.AvatarViewActivity$FetchImageAsyncTask.doInBackground(AvatarViewActivity.java:1)
11-21 20:02:30.987: E/AndroidRuntime(343):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-21 20:02:30.987: E/AndroidRuntime(343):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-21 20:02:30.987: E/AndroidRuntime(343):  ... 4 more
11-21 20:02:31.330: I/Process(61): Sending signal. PID: 343 SIG: 3
4

2 回答 2

0

像这样的东西,但我仍然建议(我为每个人都这样做)只看并使用加载器。例如AsyncTaskLoader

new AsyncTask<String, Void, Drawable>() {

    @Override
    protected Drawable doInBackground(String... params) {
        return ImageOperations(getApplicationContext(), params[0]);
    }

    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);
        ImageView icon = new ImageView(context);
        icon = (ImageView) findViewById(R.id.imageView1);
        icon.setImageDrawable(image);
    }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "HIDDEN FOR PRIVACY" + idnumber2);
于 2012-11-22T00:13:35.623 回答
0

这样的事情应该让你开始:

/** A custom AsyncTask for downloading images in the background. */
private static class FetchImageAsyncTask extends AsyncTask<Void, Void, Drawable> {
    private final String mUrl;
    private final ImageView mImageView;

    public FetchImageAsyncTask(ImageView view, String url) {
        mUrl = url;
        mImageView = view;
    }

    @Override
    protected Drawable doInBackground(Void... params) {
        // This aptly named method will run on a background Thread. This
        // means your user interface will not be interrupted while the
        // image is downloaded.

        try {
            URL url = new URL(mUrl);

            // Instead of URL#getContent() you might consider using the
            // DefaultHttpClient class.
            return Drawable.createFromStream(
                    (InputStream) url.getContent(), "src");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Drawable result) {
        // The result of doInBackground will be passed to this method, which
        // is run on the UI Thread. This is important because you cannot
        // update an ImageView with a new image from a background Thread.
        if (result != null) {
            mImageView.setImageDrawable(result);
        }
    }
}

public void firstbutton(View view) {
    InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    EditText editText = (EditText)findViewById(R.id.editText1);
    inputMgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);

    EditText idnumber=(EditText)findViewById(R.id.editText1);
    String idnumber2= idnumber.getText().toString();
    int i = Integer.parseInt(editText.getText().toString());
    idnum=i;
    setContentView(R.layout.viewer);
    Context context = view.getContext();

    // Get your image view
    ImageView iconView = (ImageView) findViewById(R.id.imageView1);

    // Create a new background task
    FetchImageAsyncTask task = new FetchImageAsyncTask(iconView, "YOUR_URL_HERE");

    // Start the background task
    task.execute();
}
于 2012-11-22T00:18:35.453 回答