Sen*_*Sen 11 android http bitmap
我想知道是否可以使用BitmapFactory.decodeFile方法解码来自http位置的图像?
例如.
ImageView imageview = new ImageView(context);
Bitmap bmp = BitmapFactory.decodeFile("http://<my IP>/test/abc.jpg");
imageview.setImageBitmap(bmp);
Run Code Online (Sandbox Code Playgroud)
但是bmp总是返回null.
有没有其他方法来实现这种情况,我的服务器PC中有一组图像,我通过xml将图像加载到我的画廊应用程序?
谢谢,
森
Ami*_*far 16
使用decodeStream并传递URL的输入流.
这是一个例子:
Bitmap bmp = BitmapFactory.decodeStream(new java.net.URL(url).openStream())
Run Code Online (Sandbox Code Playgroud)
Sen*_*Sen 10
@Amir&@Sankar:感谢您提出的宝贵建议.
我通过执行以下代码片段解决了上述问题:
ImageView iv = new ImageView(context);
try{
String url1 = "http://<my IP>/test/abc.jpg";
URL ulrn = new URL(url1);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
iv.setImageBitmap(bmp);
else
System.out.println("The Bitmap is NULL");
} catch(Exception e) {
}
Run Code Online (Sandbox Code Playgroud)
谢谢,
森
String urldisplay="http://www.google.com/";//sample url
Log.d("url_dispaly",urldisplay);
try{
InputStream in = new java.net.URL(urldisplay).openStream();
Bitmap mIcon11 = BitmapFactory.decodeStream(new SanInputStream(in));
}
catch(Exception e){}
Run Code Online (Sandbox Code Playgroud)
创建类名SanInputStream
public class SanInputStream extends FilterInputStream {
public SanInputStream(InputStream in) {
super(in);
}
public long skip(long n) throws IOException {
long m = 0L;
while (m < n) {
long _m = in.skip(n-m);
if (_m == 0L) break;
m += _m;
}
return m;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20510 次 |
| 最近记录: |