1

非同期タスクを使用して以下のコードを使用してインターネットから画像をフェッチしていますが、関数から返されるbitmpは常にnullです。

private Bitmap asyncTaskFetchImage(final String imgeurl) {
        // TODO Auto-generated method stub

        new AsyncTask<Object, Object, Object>() {

            @Override
            protected void onPreExecute() {

                progress_Dialog = ProgressDialog.show(this, "", "Loading");
            }

            @Override
            protected Object doInBackground(Object... params) {
                // TODO Auto-generated method stub
                try
                {
                toSendBg=LoadImageFromURL(imgeurl);
                System.gc();
                return 0;
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
            @Override
            protected void onPostExecute(Object result) {
                if (progress_Dialog != null) {

                    progress_Dialog.dismiss();

                }

            }

        }.execute();
        return toSendBg;
    }

これは、Asyntaskから値を返す正確な方法ですか?

4

2 に答える 2

1

以下のコードを試して、AsyncTaskを使用してWebから画像をダウンロードし、imageviewに表示してください。

public class MainActivity extends Activity {

    ImageView mImgView1;
    static Bitmap bm;
    ProgressDialog pd;
    String imageUrl = "https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg";
    BitmapFactory.Options bmOptions;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImgView1 = (ImageView) findViewById(R.id.mImgView1);
        pd = ProgressDialog.show(MainActivity.this, "Aguarde...",
                "Carregando...");
        new ImageDownload().execute("");
    }

    public class ImageDownload extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;
            loadBitmap(imageUrl, bmOptions);
            return imageUrl;
        }

        protected void onPostExecute(String imageUrl) {
            pd.dismiss();
            if (!imageUrl.equals("")) {
                mImgView1.setImageBitmap(bm);
            } else {
                Toast.makeText(MainActivity.this,
                        "Não foi possível obter resultados", Toast.LENGTH_LONG)
                        .show();
            }
        }

    }

    public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bm = BitmapFactory.decodeStream(in, null, options);
            in.close();
        } catch (IOException e1) {
        }
        return bm;
    }

    private static InputStream OpenHttpConnection(String strURL)
            throws IOException {
        InputStream inputStream = null;
        URL url = new URL(strURL);
        URLConnection conn = url.openConnection();

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = httpConn.getInputStream();
            }
        } catch (Exception ex) {
        }
        return inputStream;
    }
}
于 2012-10-27T05:10:17.163 に答える
1
private Bitmap asyncTaskFetchImage(final String imgeurl) {
    Bitmap bmp=null;
    new AsyncTask<Object, Object, Object>() {
    ...

そしてあなたのdoInBackground method変化で

return toSendBg;

@Override
protected void onPostExecute(Object result) {
    if (progress_Dialog != null) {
        progress_Dialog.dismiss();
        bmp=(Bitmap)result;
    }
}
}.execute();
return bmp;

これを試して..、。

于 2012-10-27T06:05:08.070 に答える