Android 将帖子共享到Facebook时图像丢失

Android 将帖子共享到Facebook时图像丢失,android,facebook,image,sdk,share,Android,Facebook,Image,Sdk,Share,我正在制作一个应用程序,我需要在facebook timeline上与文本共享一个图像。然而,通过我的代码,我可以将文本共享到facebook的墙上,但是共享时图像丢失了。我已经尝试了堆栈溢出的大部分代码。但是还没有成功。这是我的代码。我用这个视频作为参考 公共类MainActivity扩展了活动{ String[] ShareOption; Spinner ShareOptionList; int driverStarScore = 1; //2 or 3 ... Facebook fb;

我正在制作一个应用程序,我需要在facebook timeline上与文本共享一个图像。然而,通过我的代码,我可以将文本共享到facebook的墙上,但是共享时图像丢失了。我已经尝试了堆栈溢出的大部分代码。但是还没有成功。这是我的代码。我用这个视频作为参考

公共类MainActivity扩展了活动{

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


 }
String[]共享选项;
微调器共享选项列表;
int driverStarScore=1;//2或3。。。
Facebook fb;
列表视图列表;
字符串[]ShareItemName={
“Dropbox”,
“电子邮件”,
“Facebook”,
“谷歌+”,
“推特”,
“Whatsapp”,
};
整数[]ShareImageId={
R.drawable.ic_升降箱,
R.drawable.ic_电子邮件,
R.drawable.ic_facebook,
R.drawable.ic_谷歌+,
R.drawable.ic_推特,
R.drawable.ic_whatsapp,
};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String APP_ID=getString(R.String.APP_ID);
fb=新Facebook(应用程序ID);
ShareOptionList=(微调器)findViewById(R.id.Spinner\u ShareScore);
ShareOption=getResources().getStringArray(R.array.ShareChooseOption);
ArrayAdapter adapter1=新的ArrayAdapter(这是android.R.layout.simple\u微调器\u项,ShareOption);
ShareOptionList.setAdapter(适配器1);
ShareOptionList.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、,
整数arg2,长arg3){
//TODO自动生成的方法存根
int index=arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(),“您选择”+ShareOption[index],Toast.LENGTH\u LONG.show();
}
@凌驾
未选择公共无效(AdapterView父级){
//TODO自动生成的方法存根
}
});
ShareListAdapter=新的ShareListAdapter(这个,ShareItemName,ShareImageId);
列表=(ListView)findViewById(R.id.ListView\u共享);
list.setAdapter(适配器);
list.setOnItemClickListener(新的OnItemClickListener(){
@抑制警告(“弃用”)
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//TODO自动生成的方法存根
字符串Selecteditem=ShareItemName[+位置];
Toast.makeText(MainActivity.this,Selecteditem,Toast.LENGTH_SHORT.show();
如果(Selecteditem==“Facebook”){
Bundle params=新Bundle();
参数putString(“名称”、“评估我”);
参数putString(“标题”,“我的驾驶成绩”);
参数putString(“链接”http://www.mybringback.com/");
参数putString(“图片”http://media-cerulean.cursecdn.com/attachments/8/944/1star.png");
对话框(MainActivity.this,“提要”,参数,新的DialogListener(){
@凌驾
公开作废onFacebookError(FacebookError e){
}
@凌驾
公共无效onError(对话框错误e){
//TODO自动生成的方法存根
}
@凌驾
公开作废{
//TODO自动生成的方法存根
}
@凌驾
未完成的公共void(捆绑值){
//TODO自动生成的方法存根
}
});
}
如果(选择编辑项==“电子邮件”){
意向电子邮件=新意向(意向.行动\发送);
email.setType(“message/rfc822”);
email.putExtra(Intent.EXTRA_email,新字符串[]{”recipient@example.com“});//如果您还想添加电子邮件地址。
email.putExtra(android.content.Intent.EXTRA_TEXT,“示例文本”);
email.putExtra(Intent.EXTRA_主题,“驾驶成绩电子邮件”);
试一试{
startActivity(Intent.createChooser(电子邮件,“发送邮件…”);
}捕获(android.content.ActivityNotFoundException ex){
Toast.makeText(MainActivity.this,“没有安装电子邮件客户端。”,Toast.LENGTH_SHORT.show();
}
}
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
返回super.onOptionsItemSelected(项目);
}
}
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>

试试这个:

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

        }
    }
公共类MainActivity扩展活动{
字符串[]共享选项;
微调器共享选项列表;
int driverStarScore=1;//2或3。。。
Facebook fb;
列表视图列表;
字符串[]ShareItemName={
“Dropbox”,
“电子邮件”,
“Facebook”,
“谷歌+”,
“推特”,
“Whatsapp”,
};
整数[]ShareImageId={
R.drawable.ic_升降箱,
R.drawable.ic_电子邮件,
R.drawable.ic_facebook,
R.drawable.ic_谷歌+,
R.drawable.ic_推特,
R.drawable.ic_whatsapp,
};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String APP_ID=getString(R.String.APP_ID);
fb=新Facebook(应用程序ID);
ShareOptionList=(微调器)findViewById(R.id.Spinner\u ShareScore);
ShareOption=getResources().getStringArray(R.array.ShareChooseOption);
ArrayAdapter adapter1=新的ArrayAdapter(这是android.R.layout.simple\u微调器\u项,ShareOption);
股票期权
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

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


}