Sunday, February 19, 2012

Hackbook Upload Photo

import android.provider.MediaStore;

final static int PICK_EXISTING_PHOTO_RESULT_CODE = 1;

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();
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
/*
* if this is the activity result from authorization flow, do a call
* back to authorizeCallback Source Tag: login_tag
*/

case AUTHORIZE_ACTIVITY_RESULT_CODE: {
Utility.mFacebook.authorizeCallback(requestCode, resultCode, data);
break;
}

/*
* if this is the result for a photo picker from the gallery, upload
* the image after scaling it. You can use the Utility.scaleImage()
* function for scaling
*/

case PICK_EXISTING_PHOTO_RESULT_CODE: {
if (resultCode == Activity.RESULT_OK) {
Uri photoUri = data.getData();
if (photoUri != null) {
Bundle params = new Bundle();
try {
params.putByteArray("photo",
Utility.scaleImage(getApplicationContext(), photoUri));
} catch (IOException e) {
e.printStackTrace();
}
params.putString("caption", "FbAPIs Sample App photo upload");
Utility.mAsyncRunner.request("me/photos", params, "POST",
new PhotoUploadListener(), null);
} else {
Toast.makeText(getApplicationContext(),
"Error selecting image from the gallery.", Toast.LENGTH_SHORT)
.show();
}
} else {
Toast.makeText(getApplicationContext(), "No image selected for upload.",
Toast.LENGTH_SHORT).show();
}
break;
}

//}
}



public class PhotoUploadListener extends BaseRequestListener {

        @Override
        public void onComplete(final String response, final Object state) {
            dialog.dismiss();
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    new UploadPhotoResultDialog(Hackbook.this, "Upload Photo executed", response)
                            .show();
                }
            });
        }

        public void onFacebookError(FacebookError error) {
            dialog.dismiss();
            Toast.makeText(getApplicationContext(), "Facebook Error: " + error.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }

No comments: