给定一个图片的Url,我想下载它并将其粘贴到我的android画布上。如何将图像检索到我的应用程序?
请帮帮忙。
谢谢,德塞罗。
发布于 2009-07-22 07:52:32
您可以使用以下代码下载镜像:
URLConnection connection = uri.toURL().openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 8 * 1024);
Bitmap bmp = BitmapFactory.decodeStream(bis);
bis.close();
is.close(); 在AndroidManifest.xml中需要以下权限:
<uses-permission android:name="android.permission.INTERNET" />发布于 2011-03-05 22:10:16
别忘了给应用程序连接到Web的权限,
在AndroidManifest.xml中:
<uses-permission android:name="android.permission.INTERNET" />发布于 2009-07-22 01:23:26
安卓现在可能支持一个HTTP客户端库,但是对于任何细粒度的控件,你都可以使用URL & HttpURLConnection。代码将如下所示:
URL connectURL = new URL(<your URL goes here>);
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
// do some setup
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
// connect and flush the request out
conn.connect();
conn.getOutputStream().flush();
// now fetch the results
String response = getResponse(conn);在getResponse()看起来像这样的地方,在您的例子中,您将得到一堆二进制数据,您可能希望将StringBuffer更改为字节数组,并以更大的增量对读取进行分块。
private String getResponseOrig(HttpURLConnection conn)
{
InputStream is = null;
try
{
is = conn.getInputStream();
// scoop up the reply from the server
int ch;
StringBuffer sb = new StringBuffer();
while( ( ch = is.read() ) != -1 ) {
sb.append( (char)ch );
}
return sb.toString();
}
catch(Exception e)
{
Log.e(TAG, "biffed it getting HTTPResponse");
}
finally
{
try {
if (is != null)
is.close();
} catch (Exception e) {}
}
return "";
}当你在谈论可能很大的图像数据时,在Android中你需要努力的其他事情是确保你尽快释放你的内存,你只有16mb的堆来处理所有的应用程序,它很快就会用完,如果你不善于释放内存资源,GC会让你抓狂的
https://stackoverflow.com/questions/1162260
复制相似问题