How to allow user to post an Image on Wall using S

2019-02-27 18:12发布

问题:

I am allowing user to post a message on their [Friend & User] Wall along with single static image, and for that image i am using Web Image URL

But now i wish to allow user to choose any single image from multiple images, like 10 images stored in a particular folder in SDCard, and then post to Wall.

So here is my question, how to do that?

My existing code to post static image on Wall, read below:

@Override
public void onClick(DialogInterface dialog, int which) {

        Bundle params = new Bundle();
            params.putString("to", String.valueOf(friendId));
        params.putString("caption", getString(R.string.app_name));
        params.putString("description", getString(R.string.app_desc));
        params.putString("link", "http://www.google.com");
            params.putString("picture",FacebookUtility.HACK_ICON_URL);
            params.putString("name",getString(R.string.app_action));

            FacebookUtility.facebook.dialog(FriendsList.this,
           "feed", params, (DialogListener) new PostDialogListener());
      }
  }).setNegativeButton(R.string.no, null).show();
 } catch (JSONException e) {
      showToast("Error: " + e.getMessage());
}

FacebookUtility.java:-

public static final String HACK_ICON_URL = "http://2.bp.blogspot.com/-WuasmTMjMA4/TY0SS4TzIMI/AAAAAAAAFB4/6alyfOzWsqM/s320/flowers-wallpapers-love-blooms-roses-bunch-of-flowers.jpg";

Check existing screen of my app,

As you can see in above screen i am showing only single static image as i have written above, but now i wanna allow user to choose an image from multiple images using SD Card, my SDCard path like: /sdcard/FbImages/

now i want to know how to place button in above screen [because i am not using any custom xml for this, that's native feature in FacebookSDK]

So here is my question how to open sdcard folder and how to choose single image to post from multiple images

回答1:

you have to find path for that image.

try following code to select image

btnSelect.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             Intent intent = new Intent();
             intent.setType("image/*");
             intent.setAction(Intent.ACTION_GET_CONTENT);
             startActivityForResult(Intent.createChooser(intent,"Select Picture"), 0);
        }
    });

get path for selectd image

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 0) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);

        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

then use

  params.putString("picture",selectedImagePath);


回答2:

Thanks to notify me for answer here. As you have seen here, I am sending app request to my facebook friend. However its nothing like to post photo on Friend's wall. There you only able to post App icon with the url ASK.

If you want to post photo on Friends wall with message then please check my below answer.

Bundle param = new Bundle();
param.putString("message", "picture caption");
param.putByteArray("picture", ImageBytes);
mAsyncRunner.request("me/photos", param, "POST", new SampleUploadListener());

In my above code, ImageBytes is the byte[] of the Image. You will have to play little to select image from the perticular folder then convert that selected image in to byte[]. Use that byte[] in my above code.

Hope it will surly help you.

Enjoy Coding... :)



回答3:

use it when pick bitmap from sdcard and give the path also
past this code.
protected void share(String nameApp, String imagePath, String text) {
        // TODO Auto-generated method stub

        try {
            List<Intent> targetedShareIntents = new ArrayList<Intent>();
            Intent share = new Intent(android.content.Intent.ACTION_SEND);
            share.setType("image/jpeg");
            List<ResolveInfo> resInfo = getPackageManager()
                    .queryIntentActivities(share, 0);
            if (!resInfo.isEmpty()) {
                for (ResolveInfo info : resInfo) {
                    Intent targetedShare = new Intent(
                            android.content.Intent.ACTION_SEND);
                    targetedShare.setType("image/jpeg"); // put here your mime
                    // type
                    if (info.activityInfo.packageName.toLowerCase().contains(
                            nameApp)
                            || info.activityInfo.name.toLowerCase().contains(
                                    nameApp)) {
                        targetedShare.putExtra(Intent.EXTRA_SUBJECT, text);
                        targetedShare.putExtra(Intent.EXTRA_TEXT, text);
                        targetedShare.putExtra(Intent.EXTRA_STREAM,
                                Uri.fromFile(new File(imagePath)));
                        targetedShare.setPackage(info.activityInfo.packageName);
                        targetedShareIntents.add(targetedShare);
                    }
                }
                Intent chooserIntent = Intent.createChooser(
                        targetedShareIntents.remove(0), "Select app to share");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                        targetedShareIntents.toArray(new Parcelable[] {}));
                startActivity(chooserIntent);
            }
        } catch (Exception e) {
        }

    }