本文介绍了我怎么能在黑莓显示来自URL的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有图像的简单的URL,我想显示位图场的形象。
这是我class..But将不会给出任何结果。
i have simple url of image, i want to display that image in Bitmap Field.this is my class..But it will is not give any result.
public class UrlToImage
{
public static Bitmap _bmap;
UrlToImage(String url)
{
HttpConnection connection = null;
InputStream inputStream = null;
EncodedImage bitmap;
byte[] dataArray = null;
try
{
connection = (HttpConnection) Connector.open(url, Connector.READ, true);
inputStream = connection.openInputStream();
byte[] responseData = new byte[10000];
int length = 0;
StringBuffer rawResponse = new StringBuffer();
while (-1 != (length = inputStream.read(responseData)))
{
rawResponse.append(new String(responseData, 0, length));
}
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK)
{
throw new IOException("HTTP response code: "
+ responseCode);
}
final String result = rawResponse.toString();
dataArray = result.getBytes();
}
catch (final Exception ex)
{ }
finally
{
try
{
inputStream.close();
inputStream = null;
connection.close();
connection = null;
}
catch(Exception e){}
}
bitmap = EncodedImage.createEncodedImage(dataArray, 0,dataArray.length);
// this will scale your image acc. to your height and width of bitmapfield
int multH;
int multW;
int currHeight = bitmap.getHeight();
int currWidth = bitmap.getWidth();
multH= Fixed32.div(Fixed32.toFP(currHeight),Fixed32.toFP(480));//height
multW = Fixed32.div(Fixed32.toFP(currWidth),Fixed32.toFP(360));//width
bitmap = bitmap.scaleImage32(multW,multH);
_bmap=bitmap.getBitmap();
}
public Bitmap getbitmap()
{
return _bmap;
}
}
感谢名单提前。
推荐答案
试试这个,但这里的URL扩展是非常重要的。
Try this but here url extension is important.
如果使用WiFi那么你的手机界面=无线这是工作,否则它不会对URL扩展工作,所以普莱舍确认以下网址
if your mobile using wifi then ";interface=wifi" this is working otherwise it wont work so for url extensions plese verify following url
public static Bitmap connectServerForImage(String url)
{
HttpConnection httpConnection = null;
DataOutputStream httpDataOutput = null;
InputStream httpInput = null;
int rc;
Bitmap bitmp = null;
try
{
httpConnection = (HttpConnection) Connector.open(url+";interface=wifi");
rc = httpConnection.getResponseCode();
if (rc == HttpConnection.HTTP_OK)
{
httpInput = httpConnection.openInputStream();
InputStream inp = httpInput;
byte[] b = IOUtilities.streamToBytes(inp);
EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
bitmp=hai.getBitmap();
}else{
throw new IOException("HTTP response code: " + rc);
}
}catch (Exception ex) {
System.out.println("URL Bitmap Error........" + ex.getMessage());
} finally
{
try
{
if (httpInput != null)
httpInput.close();
if (httpDataOutput != null)
httpDataOutput.close();
if (httpConnection != null)
httpConnection.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
return bitmp;
}
这篇关于我怎么能在黑莓显示来自URL的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!