Sure.... follow their docs on how to do it. Or this...
Code:
protected File getScreenshot(View view) {
View v = view.getRootView();
Boolean oldCacheSetting = v.isDrawingCacheEnabled();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
v.setDrawingCacheEnabled(oldCacheSetting);
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, "screenshot.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return myPath;
}
public Uri getImageContentUri(File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = this.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
protected void postScreenshotToSocialize(View v) {
File myFile = getScreenshot(v);
final Uri image = getImageContentUri(myFile);
final Entity entity = Entity.newInstance("Socialize Entity name here", null);
final Activity context = this;
// First create a Socialize share object so we get the correct URLs
ShareOptions options = ShareUtils.getUserShareOptions(context);
SocialNetwork[] where_to_share = {SocialNetwork.FACEBOOK };
ShareUtils.registerShare(context, entity, options, new ShareAddListener() {
@Override
public void onError(SocializeException error) {
}
@Override
public void onCreate(Share result) {
// We have the result, use the URLs to add to the post
PropagationInfo propagationInfo = result.getPropagationInfoResponse().getPropagationInfo(ShareType.FACEBOOK);
String link = propagationInfo.getEntityUrl();
// Now post to Facebook.
Map<String, Object> postData = new HashMap<String, Object>();
try {
// Format the picture for Facebook
byte[] imageData = FacebookUtils.getImageForPost(context, image);
// Add the photo to the post
postData.put("photo", imageData);
PrefsCacheManager sharedPrefs = PrefsCacheManager.getInstance();
String msg = "Share message users see";
// Add the link returned from Socialize to use SmartDownloads
postData.put("caption", msg + link);
// Add other fields to postData as necessary
// Post to me/photos
FacebookUtils.post(context, "me/photos", postData, new SocialNetworkPostListener() {
@Override
public void onNetworkError(Activity parent, SocialNetwork network, Exception error) {
if (parent != null) {
if (!parent.isFinishing()) {
Activity_BaseClass p = (Activity_BaseClass)parent;
p.displayMessageDialog("Failure", "Facebook servers did not respond.");
}
}
}
@Override
public void onCancel() { }
@Override
public void onAfterPost(Activity parent, SocialNetwork socialNetwork, JSONObject responseObject) {
if (parent != null) {
if (!parent.isFinishing()) {
Activity_BaseClass p = (Activity_BaseClass)parent;
p.displayMessageDialog("Success", "Picture posted to Facebook!");
}
}
}
});
}
catch (Exception e) {
displayMessageDialog("Failure", "Internal error in 3rd party code. This will be fixed in an update.\n\nYour screenshot was saved to your Gallery as the file 'screenshot.jpg'");
}
}
}, where_to_share);
}
Be warned... there is a bug in Facebook's SDK. If you post a message, and while the message is being posted, the current activity closes.... the FB SDK will crash your app with a nullptr, since it does not check the Activity is still valid before calling the return callback.