问题
I used the following code, to upload a image to my Facebook wall.
try {
bitmap=BitmapFactory.decodeFile(imagePostion);
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(
"https://graph.facebook.com/me/photos?access_token="+ a);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("source", new ByteArrayBody(data, imagePostion));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,localContext);
Log.v("Response !!!!!!!!",response+"");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And I got null pointer exception at this line
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Need help.
回答1:
Here is no image bitmap to upload it. Create an upload image BitMap.
add this Line
bitmap.compress(CompressFormat.JPEG, 100, bos);
after
ByteArrayOutputStream bos = new ByteArrayOutputStream();
回答2:
Finally I get it work. The MultipartEntity works in Android 2.1 onwards. I had tried in 1.6. That is why I got that error.
来源:https://stackoverflow.com/questions/10918660/upload-a-image-contained-in-a-url-to-facebook-in-android