将帖子分享到Facebook时图片丢失了

时间:2015-06-01 09:27:00

标签: android facebook image sdk share

我正在制作一个应用程序,我需要在Facebook时间轴上与文本共享图像。然而,使用我的代码,我可以共享文本到Facebook墙,但共享时图像丢失。我已经尝试了大部分堆栈溢出代码。但还没有成功。这是我的代码。我将此视频用作参考https://www.youtube.com/watch?v=ESW5rat9m2c&index=11&list=PLE425D66F454D6AFA

  package com.example.test2;





 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;



import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;


import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

公共类MainActivity扩展了Activity {

String[] ShareOption;
Spinner ShareOptionList;
int driverStarScore = 1; //2 or 3 ...
Facebook fb;



ListView list;

 String[] ShareItemName ={

 "Dropbox",
 "Email",
 "Facebook",
 "Google Plus",
 "Twitter",
 "Whatsapp",

 };

 Integer[] ShareImageId={

         R.drawable.ic_dropbox,
         R.drawable.ic_email,
         R.drawable.ic_facebook,
         R.drawable.ic_googleplus,
         R.drawable.ic_twitter,
         R.drawable.ic_whatsapp,

         };
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String APP_ID=getString(R.string.APP_ID);
    fb=new Facebook(APP_ID);






    ShareOptionList=(Spinner) findViewById(R.id.spinner_ShareScore);
    ShareOption=getResources().getStringArray(R.array.ShareChooseOption);
    ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ShareOption);
    ShareOptionList.setAdapter(adapter1);

    ShareOptionList.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            int index=arg0.getSelectedItemPosition();
            Toast.makeText(getBaseContext(), "You select "+     ShareOption[index],Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }



    });




    ShareListAdapter adapter=new ShareListAdapter(this, ShareItemName, ShareImageId);
    list=(ListView)findViewById(R.id.listview_share);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @SuppressWarnings("deprecation")
        @Override
         public void onItemClick(AdapterView<?> parent, View view,
         int position, long id) {
         // TODO Auto-generated method stub
         String Selecteditem= ShareItemName[+position];

         Toast.makeText(MainActivity.this, Selecteditem,  Toast.LENGTH_SHORT).show();

         if(Selecteditem=="Facebook"){  

             Bundle params=new Bundle();
             params.putString("name", "ASSES ME");
             params.putString("caption", "My Driving Score");
             params.putString("link", "http://www.mybringback.com/");
             params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/944/1star.png");




            fb.dialog(MainActivity.this, "feed", params, new DialogListener(){
                 @Override
                 public void onFacebookError(FacebookError e){

                 }
                 @Override
                 public void onError(DialogError e){
                     //TODO Auto-generated method stub
                 }

                 @Override
                 public void onCancel(){
                     //TODO Auto-generated method stub
                 }
                @Override
                public void onComplete(Bundle values) {
                    // TODO Auto-generated method stub

                }

             });

         }
         if(Selecteditem=="Email"){
            Intent email = new Intent(Intent.ACTION_SEND);

            email.setType("message/rfc822");
            email.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"}); // if you want to add email address also.
            email.putExtra(android.content.Intent.EXTRA_TEXT, "Sample Text");
            email.putExtra(Intent.EXTRA_SUBJECT, "Driving Score Email");

            try {
                startActivity(Intent.createChooser(email, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }
         }

        }
         });



}



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }





  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.


    return super.onOptionsItemSelected(item);
   }


 }

的AndroidManifest.xml

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test2"
    android:versionCode="1"
    android:versionName="1.0" >

   <uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="18" />
   <uses-permission android:name="android.permission.INTERNET"/>

   <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
     </activity>
     <activity
        android:name=".Menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.Menu" />

            <data android:mimeType="image/*" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

  </application>





 </manifest>

2 个答案:

答案 0 :(得分:0)

试试这个:

Bundle params = new Bundle();
            params.putString(Facebook.TOKEN, facebook.getAccessToken());
            params.putString("method", "photos.upload");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
            mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);

    public class SampleUploadListener extends BaseKeyListener implements RequestListener
    {

        public void onComplete(final String response, final Object state)
        {
            try
            {
                // process the response here: (executed in background thread)
                Log.d("Facebook-Example", "Response: " + response.toString());
                JSONObject json = Util.parseJson(response);
                final String src = json.getString("src");

                // then post the processed result back to the UI thread
                // if we do not do this, an runtime exception will be generated
                // e.g. "CalledFromWrongThreadException: Only the original
                // thread that created a view hierarchy can touch its views."

            }
            catch (JSONException e)
            {
                Log.w("Facebook-Example", "JSON Error in response");
            }
            catch (FacebookError e)
            {
                Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
            }
        }

        public void onFacebookError(FacebookError e, Object state)
        {
            // TODO Auto-generated method stub

        }

        public Bitmap getInputType(Bitmap img)
        {
            // TODO Auto-generated method stub
            return img;
        }

        public int getInputType()
        {
            // TODO Auto-generated method stub
            return 0;
        }

        public void onIOException(IOException e, Object state)
        {
            // TODO Auto-generated method stub

        }

        public void onFileNotFoundException(FileNotFoundException e, Object state)
        {
            // TODO Auto-generated method stub

        }

        public void onMalformedURLException(MalformedURLException e, Object state)
        {
            // TODO Auto-generated method stub

        }
    }

答案 1 :(得分:0)

public class MainActivity extends Activity {




 String[] ShareOption;
 Spinner ShareOptionList;
 int driverStarScore = 1; //2 or 3 ...
 Facebook fb;



  ListView list;

   String[] ShareItemName ={

  "Dropbox",
  "Email",
  "Facebook",
  "Google Plus",
   "Twitter",
  "Whatsapp",

  };

  Integer[] ShareImageId={

         R.drawable.ic_dropbox,
         R.drawable.ic_email,
         R.drawable.ic_facebook,
         R.drawable.ic_googleplus,
         R.drawable.ic_twitter,
         R.drawable.ic_whatsapp,

         };
   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String APP_ID=getString(R.string.APP_ID);
    fb=new Facebook(APP_ID);






     ShareOptionList=(Spinner) findViewById(R.id.spinner_ShareScore);
    ShareOption=getResources().getStringArray(R.array.ShareChooseOption);
     ArrayAdapter<String> adapter1=new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item,ShareOption);
    ShareOptionList.setAdapter(adapter1);

    ShareOptionList.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            int index=arg0.getSelectedItemPosition();
            Toast.makeText(getBaseContext(), "You select "+ ShareOption[index],Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }



    });




    ShareListAdapter adapter=new ShareListAdapter(this, ShareItemName, ShareImageId);
    list=(ListView)findViewById(R.id.listview_share);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @SuppressWarnings("deprecation")
        @Override
         public void onItemClick(AdapterView<?> parent, View view,
         int position, long id) {
         // TODO Auto-generated method stub
         String Selecteditem= ShareItemName[+position];

         Toast.makeText(MainActivity.this, Selecteditem, Toast.LENGTH_SHORT).show();

         if(Selecteditem=="Facebook"){

             Bundle params = new Bundle();
             params.putString(Facebook.TOKEN, fb.getAccessToken());
             params.putString("method", "photos.upload");
 AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(fb);
             mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);

     public class SampleUploadListener extends BaseKeyListener implements RequestListener
     {

         public void onComplete(final String response, final Object state)
         {
             try
             {
                 // process the response here: (executed in background thread)
                 Log.d("Facebook-Example", "Response: " + response.toString());
                 JSONObject json = Util.parseJson(response);
                 final String src = json.getString("src");

                 // then post the processed result back to the UI thread
                 // if we do not do this, an runtime exception will be generated
                 // e.g. "CalledFromWrongThreadException: Only the original
                 // thread that created a view hierarchy can touch its views."

             }
             catch (JSONException e)
             {
                 Log.w("Facebook-Example", "JSON Error in response");
             }
             catch (FacebookError e)
             {
                 Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
             }
         }

         public void onFacebookError(FacebookError e, Object state)
         {
             // TODO Auto-generated method stub

         }

         public Bitmap getInputType(Bitmap img)
         {
             // TODO Auto-generated method stub
             return img;
         }

         public int getInputType()
         {
             // TODO Auto-generated method stub
             return 0;
         }

         public void onIOException(IOException e, Object state)
         {
             // TODO Auto-generated method stub

         }

         public void onFileNotFoundException(FileNotFoundException e, Object state)
         {
             // TODO Auto-generated method stub

         }

         public void onMalformedURLException(MalformedURLException e, Object state)
         {
             // TODO Auto-generated method stub

         }
     }



         }
         if(Selecteditem=="Email"){
            Intent email = new Intent(Intent.ACTION_SEND);

            email.setType("message/rfc822");
            email.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"}); // if you want to add email address also.
            email.putExtra(android.content.Intent.EXTRA_TEXT, "Sample Text");
            email.putExtra(Intent.EXTRA_SUBJECT, "Driving Score Email");

            try {
                startActivity(Intent.createChooser(email, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }
         }

        }
         });



  }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }





@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.


    return super.onOptionsItemSelected(item);
    }


}