Android: Uploading photos from SD Card to Facebook Wall
I'm trying to upload pictures from my SD Card to my facebook account and still got no luck. I'm not getting any errors but the pictures I'm trying to upload are not appearing on my wall. Here's my code (full code of the Main Class):
import com.facebook.android.AsyncFacebookRunner;
import c开发者_StackOverflowom.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;
}
}
}
My
mRequestListener()::onComplete()
is being called with no errors and so I'm wondering why the pictures are not displaying on my wall.
Am I missing something here?
Please help.
Thanks in advance!
put this line.
mAsyncRunner.request("me/photos", param, "POST", new mRequestListener(), null);
instead of
mAsyncRunner.request(null, param, "POST", new mRequestListener(), null);
and check photo will appear on wall or not?
add "publish_stream" in permission..
If I am not mistaken, your Bundle param is missing "method" property. Try adding:
param.putString("method", "photos.upload");
精彩评论