我正在制作一个应用程序,我需要在Facebook时间轴上与文本共享图像。然而,使用我的代码,我可以共享Facebook墙的链接,但不能共享图像。我已经尝试了大部分堆栈溢出代码。但还没有成功。这是我的代码。
MainActivity.java
package com.example.test2;
import com.facebook.android.Facebook;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
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.ListView;
import android.widget.Spinner;
import android.widget.Toast;
公共类MainActivity扩展了Activity {
String[] ShareOption;
Spinner ShareOptionList;
int driverStarScore = 1; //2 or 3 ...
Facebook facebookClient;
SharedPreferences mPrefs;
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);
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() {
@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"){
// if (driverStarScore == 1){
//Uri pngUri = Uri.parse("file//res/drawable/star_1.png");
// }
//if (driverStarScore == 2){
// Uri pngUri = Uri.parse("file//res/drawable/star_2.png");
// }
String urlToShare = "http://tinypic.com/r/16gil4y/8";
try {
Intent facebook1 = new Intent();
facebook1.setClassName("com.facebook.katana", "com.facebook.katana.activity.composer.ImplicitShareIntentHandler");
facebook1.setAction("android.intent.action.SEND");
facebook1.setType("image/png");
facebook1.putExtra("android.intent.extra.TEXT", urlToShare);
startActivity(facebook1);
} catch (Exception e) {
// If we failed (not native FB app installed), try share through SEND
Intent facebook = new Intent(Intent.ACTION_SEND);
String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
facebook = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(facebook);
}
}
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" />
<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/png" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.sample.socialshare.facebookUpload"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault.Dialog">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
</manifest>
答案 0 :(得分:6)
与Facebook分享内容的推荐且更简单的方法是使用Android Facebook SDK。 您可以在Facebook Developer网站上找到有关如何integrate it with your app和how to share的文档。
注意:直接调用共享对话框虽然可以,但是不受支持,并且可以随时中断,所以绝对不建议这样做。