如果用户没有在自己的手机上安装Facebook应用,我想在Facebook上与外部存储共享图像。我试图打开ShareDialog,但是它没有运行正确,我收到错误
facebook不允许分享图片无法通过网络或已安装的Facebook应用版本显示提供的内容。某些对话框仅支持启动API 14
我想提一下API更高14。
这是我的ShareDiaolog代码。
enter code here
package com.palabs.polygon.activities;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.facebook.share.ShareApi;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.model.ShareOpenGraphAction;
import com.facebook.share.model.ShareOpenGraphContent;
import com.facebook.share.model.ShareOpenGraphObject;
import com.facebook.share.model.SharePhoto;
import com.facebook.share.model.SharePhotoContent;
import com.facebook.share.widget.ShareDialog;
import com.palabs.polygon.R;
import java.io.File;
public class FacebookImageShare extends AppCompatActivity {
CallbackManager callbackManager;
ShareDialog shareDialog;
String path;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
Intent i=getIntent();
path=i.getStringExtra("ImagePath");
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
// this part is optional
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result) {
Log.e("xxx", "onsuccess" + result.toString());
}
@Override
public void onCancel() {
Log.e("xxx", "oncancel");
}
@Override
public void onError(FacebookException error) {
Log.e("xxx", "onerror" + error);
}
});
if (ShareDialog.canShow(ShareLinkContent.class)) {
Bitmap bitmap=BitmapFactory.decodeFile(path);
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(bitmap)
.setCaption("Effect")
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
shareDialog.show(content);
}
}
public static Uri getImageContentUri(Context context, String filePath) {
// String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.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));
cursor.close();
return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
} else {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
}
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent data)
{
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
}
}