2012-04-06 148 views
1

我使用hackbook中的代碼進行android教程,並且我已經能夠將照片上傳到我的牆上。我現在想要的是上傳從我的相機拍攝的照片,並將其上傳到某個組的相冊中。從android手機攝像頭拍攝照片並將其上傳到facebook羣組

我認爲需要修改的那部分代碼是這樣的:

/* 
* Source Tag: upload_photo You can upload a photo from the media 
* gallery or from a remote server How to upload photo: 
* https://developers.facebook.com/blog/post/498/ 
*/ 
      case 3: { 
       if (!Utility.mFacebook.isSessionValid()) { 
        Util.showAlert(this, "Warning", "You must first log in."); 
       } else { 
        dialog = ProgressDialog.show(Hackbook.this, "", 
          getString(R.string.please_wait), true, true); 
        new AlertDialog.Builder(this) 
          .setTitle(R.string.gallery_remote_title) 
          .setMessage(R.string.gallery_remote_msg) 
          .setPositiveButton(R.string.gallery_button, 
            new DialogInterface.OnClickListener() { 
             @Override 
             public void onClick(DialogInterface dialog, int which) { 
              Intent intent = new Intent(Intent.ACTION_PICK, 
                (MediaStore.Images.Media.EXTERNAL_CONTENT_URI)); 
              startActivityForResult(intent, 
                PICK_EXISTING_PHOTO_RESULT_CODE); 
             } 

            }) 
          .setNegativeButton(R.string.remote_button, 
            new DialogInterface.OnClickListener() { 
             @Override 
             public void onClick(DialogInterface dialog, int which) { 
              /* 
* Source tag: upload_photo_tag 
*/ 
              Bundle params = new Bundle(); 
              params.putString("url", 
                "http://www.facebook.com/images/devsite/iphone_connect_btn.jpg"); 
              params.putString("caption", 
                "FbAPIs Sample App photo upload"); 
              Utility.mAsyncRunner.request("me/photos", params, 
                "POST", new PhotoUploadListener(), null); 
             } 

            }).setOnCancelListener(new DialogInterface.OnCancelListener() { 
           @Override 
           public void onCancel(DialogInterface d) { 
            dialog.dismiss(); 
           } 
          }).show(); 
       } 
       break; 
      } 

所以,你能幫幫我這句話: 1)這可能嗎?或者我必須先拍照,存放在某個地方然後找到它。我想避免這種情況(我的意思是用戶 - 如果它可以通過編程的方式完成,那麼就沒有問題)。 2)我需要更改哪些參數?

回答

0

嘗試 -

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    this.imageView = (ImageView)this.findViewById(R.id.imageView1); 
    Button photoButton = (Button) this.findViewById(R.id.button1); 
    photoButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, CAMERA_REQUEST); 
     } 
    }); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_REQUEST) { 
     Bitmap bmp = intent.getExtras().get("data"); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

     bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byte[] byteArray = stream.toByteArray(); // convert camera photo to byte array 
    } 
} 

你的Facebook類 -

public void onClick(DialogInterface dialog, int which) { 
    Bundle params = new Bundle(); 

    params.putByteArray("picture", <image in bytes>); 
    params.putString("message", "Have fun"); 

    mAsyncRunner.request("me/photos", bundle, "POST", new SampleUploadListener()); 
} 
1

這是可能的。 在攝像頭方面,你應該參考Android developer site並從他們提供的,你可以弄清楚如何臨時存儲數據,並上傳PIC給Facebook我用下面的代碼

private void upload_FB(Bitmap photo2) { 
    // TODO Auto-generated method stub 
    Calendar c = Calendar.getInstance(); 
    String name = c.getTime().toString()+" Vai vajadzētu iegādāties?"; 

    AsyncFacebookRunner fruner = new AsyncFacebookRunner(facebook); 
    Log.d("adr", mCurrentPhotoPath); 
    if(photo2!=null && mCurrentPhotoPath!=null){ 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     photo2.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     byte[] bMapArray = baos.toByteArray(); 
     Bundle params = new Bundle(); 
     params.putByteArray("photo",bMapArray); 
     params.putString("caption", name); 
     params.putString("comments", ed1.getText().toString()); 
     fruner.request("me/photos",params,"POST",new PhotoUploadListener(),null); 


    } 
} 

祝你好運:)

相關問題