upload a image contained in a url to Facebook in android

限于喜欢 提交于 2019-12-13 03:57:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!