是否可以在Android中的图像按钮中将URL中的图像放入?

Sil*_*ent 2 android

我想要做的是一个数据库列表视图右侧有一个小图像按钮和文本,我希望小图像用文本文件给出的URL改变但我被卡住了,2小时规则到了

For(file lenght)所以URL是www.site.com/images/(i++).png

Cas*_*sey 10

您想要做的事情肯定是可能的,但是您需要手动获取图像并将其设置在ImageButton上.

这是一个可以用来获取图像的方法:

private Bitmap fetchImage( String urlstr )
{
    try
    {
        URL url;
        url = new URL( urlstr );

        HttpURLConnection c = ( HttpURLConnection ) url.openConnection();
        c.setDoInput( true );
        c.connect();
        InputStream is = c.getInputStream();
        Bitmap img;
        img = BitmapFactory.decodeStream( is );
        return img;
    }
    catch ( MalformedURLException e )
    {
        Log.d( "RemoteImageHandler", "fetchImage passed invalid URL: " + urlstr );
    }
    catch ( IOException e )
    {
        Log.d( "RemoteImageHandler", "fetchImage IO exception: " + e );
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

当然,您需要将此方法包装在一个线程中(在SDK 1.5中使用带有SDK 1.5的AsyncTask或者在SDK 1.5中使用UserTask),然后只需调用:

myImageButton.setImageBitmap( bitmap );
Run Code Online (Sandbox Code Playgroud)

我认为这已经回答了你的问题,如果没有,请进一步详细说明.