0

这是我从我的 android 应用程序中拍照并将其上传到 facebook 墙上的代码。

photo_up=(Button)findViewById(R.id.camera_foto_button);
photo_up.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);              
        startActivityForResult(cameraIntent, CAMERA_REQUEST); 
    }
  });


switch(requestCode){
        case CAMERA_REQUEST:{
          Bitmap bmp = (Bitmap) data.getExtras().get("data");
          ByteArrayOutputStream stream = new ByteArrayOutputStream();        
          bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);         
          byteArray = stream.toByteArray(); // convert camera photo to byte array  
          Bundle params = new Bundle();       
          params.putByteArray("picture", byteArray);      
          params.putString("message", "Have fun");       
          Utility.mAsyncRunner.request("me/photos", params,"POST", new PhotoUploadListener(), null);
        break;

拍照时,我在 2 秒后强制关闭。这是日志:

05-17 18:04:47.756: E/AndroidRuntime(272): Uncaught handler: thread Thread-8 exiting due to uncaught exception
05-17 18:04:47.756: E/AndroidRuntime(272): java.lang.NullPointerException
05-17 18:04:47.756: E/AndroidRuntime(272):  at com.myco.android.myapp.Gallery1$PhotoUploadListener.onComplete(Gallery1.java:368)
05-17 18:04:47.756: E/AndroidRuntime(272):  at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:254)
E/dalvikvm(272): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

此外,图像没有保存在 SD 卡中,我也想要。所有必需的权限都已添加到清单中。

4

1 回答 1

0

试试这个代码,它对我有用

private void uploadImage() {

        if (facebook.isSessionValid()) {

            byte[] data = null;

            Bitmap bi = BitmapFactory.decodeFile(final_image_bitmap);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            data = baos.toByteArray();

            Bundle params = new Bundle();
            params.putString("method", "photos.upload");
            params.putByteArray("picture", data);

            AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
            mAsyncRunner.request(null, params, "POST",
                    new SampleUploadListener(), null);

        } else {
            Log.v("Session is not valid.", "");
        }
于 2012-05-17T18:29:15.867 回答