我正在尝试将SD卡中的图片上传到我的Facebook帐户,但仍然没有运气。 我没有收到任何错误,但我试图上传的图片没有出现在我的墙上。 这是我的代码(主类的完整代码):
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.Facebook.DialogListener;
public class Gallery extends Activity {
private static final String TAG = "Gallery";
// Constants
private static final int UPDATE_GRID_VIEW = 0;
// References to our images
private ArrayList<Uri> picUri = new ArrayList<Uri>();
private ArrayList<String> picName = new ArrayList<String>();
private GridView mGridview;
private ImageAdapter mImageAdapter;
// For FaceBook
private Facebook facebook;
private String fb_AppId = "MY_APP_ID";
// Progress Dialog
private ProgressDialog progressD;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.gallery);
initializeViews();
initFacebook();
authorizeFbUser();
}
public void initializeViews(){
PictureListThread picThread = new PictureListThread();
picThread.start();
mImageAdapter = new ImageAdapter(this);
mGridview = (GridView) findViewById(R.id.gallery_gridview);
mGridview.setAdapter(mImageAdapter);
mGridview.setOnItemClickListener(mOnItemClickListener);
}
// Facebook Methods and Classes
public void initFacebook(){
facebook = new Facebook(fb_AppId);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
public void authorizeFbUser(){
facebook.authorize(this, new String[] { "email", "read_stream" },
new DialogListener() {
@Override
public void onComplete(Bundle values) {
Log.d(TAG, "******************* FACEBOOK::authorize::onComplete *******************");
}
@Override
public void onFacebookError(FacebookError error) {
Log.d(TAG, "******************* FACEBOOK::authorize::onFacebookError *******************");
}
@Override
public void onError(DialogError e) {
Log.d(TAG, "******************* FACEBOOK::authorize::onError *******************");
}
@Override
public void onCancel() {
Log.d(TAG, "******************* FACEBOOK::authorize::onCancel *******************");
}
});
}
public class mRequestListener implements RequestListener{
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onMalformedURLException *******************");
}
@Override
public void onIOException(IOException e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onIOException *******************");
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onFileNotFoundException *******************");
}
@Override
public void onFacebookError(FacebookError e, Object state) {
Log.d(TAG, "******************* FACEBOOK::onFacebookError *******************");
}
@Override
public void onComplete(String response, Object state) {
Log.d(TAG, "******************* FACEBOOK::onComplete *******************");
}
}
// Methods
private Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) {
if(msg.what == UPDATE_GRID_VIEW) {
mImageAdapter.notifyDataSetChanged();
}
}
};
// Inner Classes
private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.d(TAG,"[][][][][][][][][][]-------------> PATH: "+picUri.get(position).toString());
byte[] data = null;
Bitmap bi = BitmapFactory.decodeFile(picUri.get(position).toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Log.d(TAG,"[][][][][][][][][][]-------------> DATA: "+data);
Bundle param = new Bundle();
param.putString("method", "photos.upload");
param.putString("message", picName.get(position));
param.putByteArray("picture", data);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/photos", param, "POST", new mRequestListener(), null);
Log.d(TAG,"[][][][][][][][][][]-------------> Gallery::mOnItemClickListener END!");
}
};
private class PictureListThread extends Thread {
@Override
public void run() {
Log.d(TAG, "******************* PictureListThread::run() STARTED! *******************");;
System.gc();
String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.TITLE};
Cursor picturecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
proj, null, null, MediaStore.Images.Media.TITLE);
if (picturecursor == null)
return;
if (!picturecursor.moveToFirst())
return;
do {
String picTitle = picturecursor.getString(picturecursor.getColumnIndex(MediaStore.Images.Media.TITLE));
if(picTitle.contains("KEYWORD")){
picName.add(picTitle);
picUri.add(Uri.parse(picturecursor.getString(picturecursor.getColumnIndex(MediaStore.Images.Media.DATA))));
Log.d(TAG,"[][][][][][][][][][]-------------> PICTURE ADDED: "+picTitle);
mHandler.sendEmptyMessage(UPDATE_GRID_VIEW);
}
} while (picturecursor.moveToNext());
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return picUri.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(150, 100));
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageURI(picUri.get(position));
return imageView;
}
}
}
我的
正在调用mRequestListener()::的onComplete()
没有错误,所以我想知道为什么图片没有显示在我的墙上。
我在这里遗漏了什么吗?
请帮忙。
提前致谢!
答案 0 :(得分:2)
把这一行。
mAsyncRunner.request("me/photos", param, "POST", new mRequestListener(), null);
而不是
mAsyncRunner.request(null, param, "POST", new mRequestListener(), null);
并且检查照片是否会出现在墙上?
在权限中添加“publish_stream”。
答案 1 :(得分:1)
如果我没弄错,你的Bundle param缺少“方法”属性。尝试添加:
param.putString("method", "photos.upload");