我想从URL获取位图,如
"https://graph.facebook.com/100003506521332/picture"
我试过了how-i-can-display-images-from-url-in-blackberry。但它显示http错误302.It不显示位图。如何解决问题?。
答案 0 :(得分:0)
String url = "https://graph.facebook.com/100003506521332/picture";
try
{
bitmap = globleDeclaration.getLiveImage(url, 50, 50);
bitmapField.setBitmap(bitmap);
}
catch (IOException e)
{
e.printStackTrace();
Dialog.alert(e.getMessage());
}
public Bitmap getLiveImage(String url,int width,int height) throws IOException
{
Bitmap bitmap = null;
try
{
byte[] responseData = new byte[10000];
int length = 0;
StringBuffer rawResponse = new StringBuffer();
httpconnection = (HttpConnection) Connector.open(url, Connector.READ, true);
String location=httpconnection.getHeaderField("location");
if(location!=null){
httpconnection = (HttpConnection) Connector.open(location, Connector.READ, true);
}else{
httpconnection = (HttpConnection) Connector.open(url, Connector.READ, true);
}
inputStream = httpconnection.openInputStream();
while (-1 != (length = inputStream.read(responseData)))
{
rawResponse.append(new String(responseData, 0, length));
}
int responseCode = httpconnection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK)
{
throw new IOException("HTTP response code: "
+ responseCode);
}
final String result = rawResponse.toString();
byte[] dataArray = result.getBytes();
encodeImageBitmap = EncodedImage.createEncodedImage(dataArray, 0, dataArray.length);
}
catch (final Exception ex)
{
System.out.print("Exception (" + ex.getClass() + "): " + ex.getMessage());
}
finally
{
try
{
inputStream.close();
inputStream = null;
httpconnection.close();
httpconnection = null;
}
catch(Exception e){}
}
return bitmap;
}
答案 1 :(得分:0)
如果您从上面的答案中提到的代码responce
中获得了httpconnection = (HttpConnection) Connector.open(url, Connector.READ, true);
,那么您也可以使用替代解决方案..
你可以在http://graph.facebook.com/100003506521332/?fields=picture&type=large
传递你的身份证明,而不是像{"picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/70678_100003506521332_1415569305_n.jpg"}
那样得到一个json回复
解析这个json并得到你想要的exaclty ...而不是你将使用httpconnection = (HttpConnection) Connector.open(url, Connector.READ, true);
得到答案。
如果您不想要larger
图像而不是从上面的网址中移除&type=large
。
答案 2 :(得分:0)
//YOUR PACKAGE NAME
package ...........;
//*************************
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.system.Bitmap;
public class Util_ImageLoader {
public static Bitmap getImageFromUrl(String url) {
Bitmap bitmap = null;
try {
String bitmapData = getDataFromUrl(url);
bitmap = Bitmap.createBitmapFromBytes(bitmapData.getBytes(), 0,
bitmapData.length(), 1);
} catch (Exception e1) {
e1.printStackTrace();
}
return bitmap;
}
private static String getDataFromUrl(String url) {
StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
long len = 0;
int ch = 0;
try {
c = (HttpConnection) Connector.open(url);
is = c.openInputStream();
len = c.getLength();
if (len != -1) {
for (int i = 0; i < len; i++)
if ((ch = is.read()) != -1) {
b.append((char) ch);
}
} else {
while ((ch = is.read()) != -1) {
len = is.available();
b.append((char) ch);
}
}
is.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
return b.toString();
}
}
将此复制到您的包裹.... 现在,要使用此类,请执行此操作&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
创建BITMAP对象:
Bitmap IMAGE;
之后:
IMAGE=Util_ImageLoader.getImageFromUrl("https://graph.facebook.com/100003506521332/picture");
顺便说一下,我觉得你错过了延期 像你的网址上的JPG,PNG ....检查
现在,将您的位图添加到要显示图像的位置......
享受!
add(IMAGE);