分享照片到Facebook

时间:2015-12-06 15:27:42

标签: android facebook

我在活动2中有2个活动,用户可以拍照或者可以选择活动中的图片,他们可以在图片中写文字,活动一个是用文字显示照片,但我的问题是我不能保存带有文字的图片,我无法分享 Facebook 可以请任何人帮助我,代码如下:

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Teksti qe tregon qe kliko ne te per te kalu ne aktivitetin tjeter "Toast"
    Toast.makeText(getApplicationContext(), "Press The Photo For Customize", Toast.LENGTH_LONG).show();

    //Merr intentin
    Intent intent = getIntent();

    //Vendos tekstin emrin se kush e ka ditelindjen
    String message = intent.getStringExtra(MESSAGE_KEY);
    TextView textView = (TextView) findViewById(R.id.nameChange);
    textView.setText(message);

    // Vendos tekstin From
    String message1 = intent.getStringExtra(MESSAGE_KEY1);
    TextView textView1 = (TextView) findViewById(R.id.FromName);
    textView1.setText(message1);

    // Vendos Foton
    Bundle extras = getIntent().getExtras();
    if (extras == null) {
        return;
    }
    int res = extras.getInt("resourseInt");
    ImageView view = (ImageView) findViewById(R.id.cakeView);

    view.setImageResource(res);

    //vendos foton qe vjen nga galeria
    File picture = (File) getIntent().getExtras().get("image_path");
    if (picture != null) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 3;

        Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(picture), options);

        try {
            // Jepja orientimin
            ExifInterface exif = new ExifInterface(String.valueOf(picture));
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

            // Determine Rotation
            int rotation = 0;
            if (orientation == 6) rotation = 90;
            else if (orientation == 3) rotation = 180;
            else if (orientation == 8) rotation = 270;

            // ktheje nqs eshte e detyrueshme
            if (rotation != 0) {
                // Create Matrix
                Matrix matrix = new Matrix();
                matrix.postRotate(rotation);

                // Rotate Bitmap
                Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

                // Pretend none of this ever happened!
                bitmap.recycle();
                bitmap = rotated;
                rotated = null;
            }
        } catch (Exception e) {

        }
        view.setImageBitmap(bitmap);
    }
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
}

//krijimi i menus share
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.my_menu, menu);
    return true;
}

// kur klienti klikon ne butonin qe ndodhet tek menuja
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //per menun qe te dallojm kur dikush klikon ne te ti ndajm zakonisht kur jan me shum se 1
    if (id == R.id.id_share) {
        registerForContextMenu(findViewById(R.id.id_share));
        openContextMenu(findViewById(R.id.id_share));
    }
    return true;
}

//konteksi qe fut listen qe deshiron tek butoni share
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.item_contect, menu);
}

//i ben share fotos per ne facebook
private void sharePhotoToFacebook() {

    ImageView test = (ImageView) findViewById(R.id.cakeView);

        Bitmap image = BitmapFactory.decodeFile(String.valueOf(test));
        SharePhoto photo = new SharePhoto.Builder()
                .setBitmap(image)
                .build();
        SharePhotoContent content = new SharePhotoContent.Builder()
                .addPhoto(photo)
                .build();

        ShareApi.share(content, null);
}

// kjo eshte nje pjese e facebook sdk
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
    super.onActivityResult(requestCode, responseCode, data);
    callbackManager.onActivityResult(requestCode, responseCode, data);
}

@Override
public boolean onContextItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.facebook) {
        FacebookSdk.sdkInitialize(getApplicationContext());

        callbackManager = CallbackManager.Factory.create();

        List<String> permissionNeeds = Arrays.asList("publish_actions");

        //kjo te ndihmon qe te mos kete nevoj klienti te kyqet
        loginManager = LoginManager.getInstance();

        loginManager.logInWithPublishPermissions(this, permissionNeeds);
        loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                sharePhotoToFacebook();
                Toast.makeText(getApplicationContext(), "The Photo Is Now Share On Facebook", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancel() {
                System.out.println("onCancel");
            }

            @Override
            public void onError(FacebookException error) {
                System.out.println("onError");
            }
        });

        if (id == R.id.instagram) {

        }
    }
    return true;
}

//Kliko ne Foto Onclick
public void clickPhoto(View view) {
    Intent intent = new Intent(this, SecondActivity.class);
    startActivity(intent);
}

1 个答案:

答案 0 :(得分:0)

现在照片分享比以前简单得多。 请关注最新的Facebook分享Facebook Share

** ShareLinkContent sh = new ShareLinkContent.Builder()
                    .setContentUrl(Uri.parse(mImageUrl))
                    .setContentTitle(mPostingMessage)
                    .setImageUrl(Uri.parse(mImageUrl)).build();**