Bitmap gets null when getting facebook profile picture - android

I'm trying to get my facebook profile picture by
URL fb_pic = new URL("http://graph.facebook.com/"+(facebookuserID)+"/picture?style=small" );
Ihave successfully got the image url.But when i try to convert the url into Bitmap i'm not getting response,
Bitmap bit = BitmapFactory.decodeStream(fb_pic.openConnection().getInputStream());
Please give me a solution...!!!

This code is working for me
HttpGet httpRequest = new HttpGet(URI.create(linkUrl) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entrty = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
bmap = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();

Related

Get the XML entity passed in body - HTTP Put method in Android

I wanted to retrieve the XML entity passed in body of HTTP Put method. I used the below code,
DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
conn.bind(serverSocket.accept(), new BasicHttpParams());
HttpRequest request = conn.receiveRequestHeader();
conn.receiveRequestEntity((HttpEntityEnclosingRequest)request);
HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
System.out.println(EntityUtils.toString(entity));
I could get the norma strings, but when trying to pass the XML entity, I could not even see the print statement.
I hope this code help you.
HttpPost httppost = new HttpPost(SERVICE_EPR);
StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httppost.setEntity(se);
HttpClient httpclient = new DefaultHttpClient();
BasicHttpResponse httpResponse =
(BasicHttpResponse) httpclient.execute(httppost);
response.put("HTTPStatus",httpResponse.getStatusLine().toString());
PFB the solution,
DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
params = new BasicHttpParams();
conn.bind(socket, params);
//Extracting the information from the requested URI
HttpRequest request = conn.receiveRequestHeader();
conn.receiveRequestEntity((HttpEntityEnclosingRequest)request);
HttpEntity httpEntity = ((HttpEntityEnclosingRequest)request).getEntity();
if(httpEntity != null){
InputStream instream = httpEntity.getContent();
try {
// do something useful
String myString = IOUtils.toString(instream, "UTF-8");
Log.e(TAG,">>>> http body > "+myString);
} finally {
instream.close();
}
}
Don't put any log or print statements using the data you received, I was getting 'Content already consumed' exception. I had used several logs using the httpEntity i received and that was causing the problem for me. I commented those(marked yellow) and started working.
PF the Apache doc for reference,
http://hc.apache.org/httpcomponents-core-ga/tutorial/pdf/httpcore-tutorial.pdf

How to get Bitmap from URL that redirect to https URL

Hello I am trying to get Bitmap from URL, Here this URL is redirecting to the another https URL.
URL : http://graph.facebook.com/244054592454345/picture
I am trying to get bitmap like below but this is not working for me, tried this Android : Getting a Bitmap from a url connection (graph.facebook.com) that redirects to another url but not worked for me.
Using below code, I am always getting null in the bitmap.
URL imgUrl = new URL("http://graph.facebook.com/{user-id}/picture?type=large");
InputStream in = (InputStream) imgUrl.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(in);
Any idea to get bitmap from a link which redirect to another as like me ?
This below code solved my problem and working fine.
URL fbAvatarUrl = new URL("http://graph.facebook.com/"+userProfileModel.getFacebookID()+"/picture");
HttpGet httpRequest = new HttpGet(fbAvatarUrl.toString());
DefaultHttpClient httpclient = HttpClient.getInstance();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Bitmap fbAvatarBitmap = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();

Uploading pictures with additional data to php

I want to upload a photo from the phone with some data like name and email.
From an Android device I know how to upload a photo and I know how to send data between the phone and the server but how do you do both at the same time?
Should I do them separately?
In your case you should use the MultipartEntity class,
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("name", new StringBody(name));
reqEntity.addPart("email", new StringBody(email));
if(imagePath.trim().length() != 0) {
reqEntity.addPart("profilePic", new FileBody(new File(imagePath)));
}
HttpClient hc = new DefaultHttpClient();
HttpPost postMethod = new HttpPost(urlString);
HttpEntity resEntity;
HttpResponse response = null;
postMethod.setEntity(reqEntity);
response = hc.execute(postMethod);
resEntity = response.getEntity();
response_str = EntityUtils.toString(resEntity);

Display image in android with spaces in filename from online

I need to display an image in my android application. The problem is I want to get this image from somewhere online. The URL is like:
http://www.mydomain.com/hello world image.png
As you see the image name containing some spaces in it. Every time I execute my code this will show me exception of FileNotFound. and nothing happens.
Following is my code
String imagePathCon = "hello world image.png";
String imagePath = "http://www.mydomain.com/" + imagePathCon;
try {
URL url;
url = new URL(imagePath);
// url = new URL("http://www.azuma-kinba.com/wp-content/uploads/2012/05/Android-Make-Google-Loss-in-2010.png");
InputStream content = (InputStream)url.getContent();
Drawable d = Drawable.createFromStream(content , "src");
im.setImageDrawable(d);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I use to replace space with "+" but nothing happens.
This will work.
String imagePathCon = "hello world image.png";
imagePathCon=imagePathCon.replaceAll(" ", "%20");
String imagePath = "http://www.mydomain.com/" + imagePathCon;
You must know http://en.wikipedia.org/wiki/Percent-encoding#Character_data
plese use correct Url for getting image and use below code that would definetly help u...
replace space of URL using...
imagePath=imagePath.replaceAll(" ", "%20");
and now...
HttpGet httpRequest = new HttpGet(new URL(params[0]).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream is = bufHttpEntity.getContent();
//image_value = new URL("image Url is here");
bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
//imageLoader is object of iamge view
imageLoader.setImageBitmap(bm);
You need to url encode the path, like this:
String imagePathCon = URLEncoder.encode("hello world image.png", "UTF-8");
You can also use an http get request:
The simplest way is to simply call URLEncoder which will automatically replace all charaters in your string to the url encoded format.
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
HttpConnectionParams.setSoTimeout(httpParameters, 30000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpGet httpGet = new HttpGet(URLEncoder.encode(url, "UTF-8"));
HttpResponse httpResponse = httpclient.execute(httpGet);
in = httpResponse.getEntity().getContent();
//Bitmap bmp = BitmapFactory.decodeStream(instream);
//create a bitmap or an image from the input stream

Sending Video using HTTP Post Multipart entity

What I want I want to send a video from my SDcard to a server. I also want to send some parameters/value with it.
I have tried I have tried the following code:
public String SendToServer(String aUrl,File aFile)
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(aUrl);
try
{
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", new FileBody(aFile));
entity.addPart("video[title]", new StringBody("testVideo"));
entity.addPart("video[type]", new StringBody("1"));
httpPost.setEntity(entity);
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, Globals.sessionCookie);
HttpResponse response = httpClient.execute(httpPost, localContext);
HttpEntity resEntity = response.getEntity();
String Response = "";
if (response != null)
{
Response = EntityUtils.toString(resEntity);
}
return Response;
}
catch (IOException e)
{
e.printStackTrace();
}
return "Exception";
}
What is the problem When I run this code, I get stuck at this line
HttpResponse response = httpClient.execute(httpPost, localContext);
I get no exception, no response nothing at all. Can anyone please guide me, what is the problem in here?
The above code in my question was perfect, but I had the network problem. My device was connected to a hotspot(Connectify Software). When I connected to the original network, this code worked perfect.
I recommend you people to never trust a hotspot for this kind of functionality.
try using this way if want to send as content or esle I will upload the project by tonight
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(filePath), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);

Categories

Resources